fmodule.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 amodulename,afilename: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,symmangledname: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 amodulename,afilename:string;_is_unit:boolean);
  405. var
  406. n,fn:string;
  407. begin
  408. if amodulename='' then
  409. n:=ChangeFileExt(ExtractFileName(afilename),'')
  410. else
  411. n:=amodulename;
  412. if afilename='' then
  413. fn:=amodulename
  414. else
  415. fn:=afilename;
  416. { Programs have the name 'Program' to don't conflict with dup id's }
  417. if _is_unit then
  418. inherited create(amodulename)
  419. else
  420. inherited create('Program');
  421. mainsource:=stringdup(fn);
  422. { Dos has the famous 8.3 limit :( }
  423. {$ifdef shortasmprefix}
  424. asmprefix:=stringdup(FixFileName('as'));
  425. {$else}
  426. asmprefix:=stringdup(FixFileName(n));
  427. {$endif}
  428. setfilename(fn,true);
  429. localunitsearchpath:=TSearchPathList.Create;
  430. localobjectsearchpath:=TSearchPathList.Create;
  431. localincludesearchpath:=TSearchPathList.Create;
  432. locallibrarysearchpath:=TSearchPathList.Create;
  433. localframeworksearchpath:=TSearchPathList.Create;
  434. used_units:=TLinkedList.Create;
  435. dependent_units:=TLinkedList.Create;
  436. resourcefiles:=TCmdStrList.Create;
  437. linkunitofiles:=TLinkContainer.Create;
  438. linkunitstaticlibs:=TLinkContainer.Create;
  439. linkunitsharedlibs:=TLinkContainer.Create;
  440. linkotherofiles:=TLinkContainer.Create;
  441. linkotherstaticlibs:=TLinkContainer.Create;
  442. linkothersharedlibs:=TLinkContainer.Create;
  443. linkotherframeworks:=TLinkContainer.Create;
  444. mainname:=nil;
  445. FImportLibraryList:=TFPHashObjectList.Create(true);
  446. crc:=0;
  447. interface_crc:=0;
  448. indirect_crc:=0;
  449. flags:=0;
  450. scanner:=nil;
  451. unitmap:=nil;
  452. unitmapsize:=0;
  453. derefmap:=nil;
  454. derefmapsize:=0;
  455. derefmapcnt:=0;
  456. derefdata:=TDynamicArray.Create(1024);
  457. derefdataintflen:=0;
  458. deflist:=TFPObjectList.Create(false);
  459. symlist:=TFPObjectList.Create(false);
  460. wpoinfo:=nil;
  461. checkforwarddefs:=TFPObjectList.Create(false);
  462. extendeddefs := TFPHashObjectList.Create(true);
  463. globalsymtable:=nil;
  464. localsymtable:=nil;
  465. globalmacrosymtable:=nil;
  466. localmacrosymtable:=nil;
  467. loaded_from:=LoadedFrom;
  468. do_reload:=false;
  469. do_compile:=false;
  470. sources_avail:=true;
  471. mainfilepos.line:=0;
  472. mainfilepos.column:=0;
  473. mainfilepos.fileindex:=0;
  474. recompile_reason:=rr_unknown;
  475. in_interface:=true;
  476. in_global:=true;
  477. is_unit:=_is_unit;
  478. islibrary:=false;
  479. ispackage:=false;
  480. is_dbginfo_written:=false;
  481. mode_switch_allowed:= true;
  482. moduleoptions:=[];
  483. deprecatedmsg:=nil;
  484. _exports:=TLinkedList.Create;
  485. dllscannerinputlist:=TFPHashList.Create;
  486. asmdata:=TAsmData.create(realmodulename^);
  487. InitDebugInfo(self);
  488. end;
  489. destructor tmodule.Destroy;
  490. var
  491. i : longint;
  492. begin
  493. if assigned(unitmap) then
  494. freemem(unitmap);
  495. if assigned(derefmap) then
  496. begin
  497. for i:=0 to derefmapcnt-1 do
  498. stringdispose(derefmap[i].modulename);
  499. freemem(derefmap);
  500. end;
  501. if assigned(_exports) then
  502. _exports.free;
  503. if assigned(dllscannerinputlist) then
  504. dllscannerinputlist.free;
  505. if assigned(scanner) then
  506. begin
  507. { also update current_scanner if it was pointing
  508. to this module }
  509. if current_scanner=tscannerfile(scanner) then
  510. current_scanner:=nil;
  511. tscannerfile(scanner).free;
  512. end;
  513. if assigned(asmdata) then
  514. begin
  515. if current_asmdata=asmdata then
  516. current_asmdata:=nil;
  517. asmdata.free;
  518. end;
  519. if assigned(procinfo) then
  520. begin
  521. if current_procinfo=tprocinfo(procinfo) then
  522. begin
  523. current_procinfo:=nil;
  524. current_structdef:=nil;
  525. current_genericdef:=nil;
  526. current_specializedef:=nil;
  527. end;
  528. { release procinfo tree }
  529. tprocinfo(procinfo).destroy_tree;
  530. end;
  531. DoneDebugInfo(self);
  532. used_units.free;
  533. dependent_units.free;
  534. resourcefiles.Free;
  535. linkunitofiles.Free;
  536. linkunitstaticlibs.Free;
  537. linkunitsharedlibs.Free;
  538. linkotherofiles.Free;
  539. linkotherstaticlibs.Free;
  540. linkothersharedlibs.Free;
  541. linkotherframeworks.Free;
  542. stringdispose(mainname);
  543. FImportLibraryList.Free;
  544. extendeddefs.Free;
  545. stringdispose(objfilename);
  546. stringdispose(asmfilename);
  547. stringdispose(ppufilename);
  548. stringdispose(importlibfilename);
  549. stringdispose(staticlibfilename);
  550. stringdispose(sharedlibfilename);
  551. stringdispose(exefilename);
  552. stringdispose(outputpath);
  553. stringdispose(path);
  554. stringdispose(realmodulename);
  555. stringdispose(mainsource);
  556. stringdispose(asmprefix);
  557. stringdispose(deprecatedmsg);
  558. localunitsearchpath.Free;
  559. localobjectsearchpath.free;
  560. localincludesearchpath.free;
  561. locallibrarysearchpath.free;
  562. localframeworksearchpath.free;
  563. {$ifdef MEMDEBUG}
  564. memsymtable.start;
  565. {$endif}
  566. derefdata.free;
  567. deflist.free;
  568. symlist.free;
  569. wpoinfo.free;
  570. checkforwarddefs.free;
  571. globalsymtable.free;
  572. localsymtable.free;
  573. globalmacrosymtable.free;
  574. localmacrosymtable.free;
  575. {$ifdef MEMDEBUG}
  576. memsymtable.stop;
  577. {$endif}
  578. stringdispose(modulename);
  579. inherited Destroy;
  580. end;
  581. procedure tmodule.reset;
  582. var
  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. tprocinfo(procinfo).destroy_tree;
  605. end;
  606. if assigned(asmdata) then
  607. begin
  608. if current_asmdata=TAsmData(asmdata) then
  609. current_asmdata:=nil;
  610. asmdata.free;
  611. asmdata:=nil;
  612. end;
  613. DoneDebugInfo(self);
  614. globalsymtable.free;
  615. globalsymtable:=nil;
  616. localsymtable.free;
  617. localsymtable:=nil;
  618. globalmacrosymtable.free;
  619. globalmacrosymtable:=nil;
  620. localmacrosymtable.free;
  621. localmacrosymtable:=nil;
  622. deflist.free;
  623. deflist:=TFPObjectList.Create(false);
  624. symlist.free;
  625. symlist:=TFPObjectList.Create(false);
  626. wpoinfo.free;
  627. wpoinfo:=nil;
  628. checkforwarddefs.free;
  629. checkforwarddefs:=TFPObjectList.Create(false);
  630. derefdata.free;
  631. derefdata:=TDynamicArray.Create(1024);
  632. if assigned(unitmap) then
  633. begin
  634. freemem(unitmap);
  635. unitmap:=nil;
  636. end;
  637. if assigned(derefmap) then
  638. begin
  639. for i:=0 to derefmapcnt-1 do
  640. stringdispose(derefmap[i].modulename);
  641. freemem(derefmap);
  642. derefmap:=nil;
  643. end;
  644. unitmapsize:=0;
  645. derefmapsize:=0;
  646. derefmapcnt:=0;
  647. derefdataintflen:=0;
  648. sourcefiles.free;
  649. sourcefiles:=tinputfilemanager.create;
  650. asmdata:=TAsmData.create(realmodulename^);
  651. InitDebugInfo(self);
  652. _exports.free;
  653. _exports:=tlinkedlist.create;
  654. dllscannerinputlist.free;
  655. dllscannerinputlist:=TFPHashList.create;
  656. used_units.free;
  657. used_units:=TLinkedList.Create;
  658. dependent_units.free;
  659. dependent_units:=TLinkedList.Create;
  660. resourcefiles.Free;
  661. resourcefiles:=TCmdStrList.Create;
  662. linkunitofiles.Free;
  663. linkunitofiles:=TLinkContainer.Create;
  664. linkunitstaticlibs.Free;
  665. linkunitstaticlibs:=TLinkContainer.Create;
  666. linkunitsharedlibs.Free;
  667. linkunitsharedlibs:=TLinkContainer.Create;
  668. linkotherofiles.Free;
  669. linkotherofiles:=TLinkContainer.Create;
  670. linkotherstaticlibs.Free;
  671. linkotherstaticlibs:=TLinkContainer.Create;
  672. linkothersharedlibs.Free;
  673. linkothersharedlibs:=TLinkContainer.Create;
  674. linkotherframeworks.Free;
  675. linkotherframeworks:=TLinkContainer.Create;
  676. stringdispose(mainname);
  677. FImportLibraryList.Free;
  678. FImportLibraryList:=TFPHashObjectList.Create;
  679. do_compile:=false;
  680. do_reload:=false;
  681. interface_compiled:=false;
  682. in_interface:=true;
  683. in_global:=true;
  684. mode_switch_allowed:=true;
  685. stringdispose(deprecatedmsg);
  686. moduleoptions:=[];
  687. is_dbginfo_written:=false;
  688. crc:=0;
  689. interface_crc:=0;
  690. indirect_crc:=0;
  691. flags:=0;
  692. mainfilepos.line:=0;
  693. mainfilepos.column:=0;
  694. mainfilepos.fileindex:=0;
  695. recompile_reason:=rr_unknown;
  696. {
  697. The following fields should not
  698. be reset:
  699. mainsource
  700. state
  701. loaded_from
  702. sources_avail
  703. }
  704. end;
  705. procedure tmodule.adddependency(callermodule:tmodule);
  706. begin
  707. { This is not needed for programs }
  708. if not callermodule.is_unit then
  709. exit;
  710. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  711. dependent_units.concat(tdependent_unit.create(callermodule));
  712. end;
  713. procedure tmodule.flagdependent(callermodule:tmodule);
  714. var
  715. pm : tdependent_unit;
  716. begin
  717. { flag all units that depend on this unit for reloading }
  718. pm:=tdependent_unit(current_module.dependent_units.first);
  719. while assigned(pm) do
  720. begin
  721. { We do not have to reload the unit that wants to load
  722. this unit, unless this unit is already compiled during
  723. the loading }
  724. if (pm.u=callermodule) and
  725. (pm.u.state<>ms_compiled) then
  726. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  727. else
  728. if pm.u.state=ms_second_compile then
  729. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  730. else
  731. begin
  732. pm.u.do_reload:=true;
  733. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  734. end;
  735. pm:=tdependent_unit(pm.next);
  736. end;
  737. end;
  738. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  739. var
  740. pu : tused_unit;
  741. begin
  742. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  743. used_units.concat(pu);
  744. addusedunit:=pu;
  745. end;
  746. procedure tmodule.updatemaps;
  747. var
  748. oldmapsize : longint;
  749. hp : tmodule;
  750. i : longint;
  751. begin
  752. { Extend unitmap }
  753. oldmapsize:=unitmapsize;
  754. unitmapsize:=loaded_units.count;
  755. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  756. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  757. { Extend Derefmap }
  758. oldmapsize:=derefmapsize;
  759. derefmapsize:=loaded_units.count;
  760. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  761. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  762. { Add all units to unitmap }
  763. hp:=tmodule(loaded_units.first);
  764. i:=0;
  765. while assigned(hp) do
  766. begin
  767. if hp.moduleid>=unitmapsize then
  768. internalerror(200501151);
  769. { Verify old entries }
  770. if (i<oldmapsize) then
  771. begin
  772. if (hp.moduleid<>i) or
  773. (unitmap[hp.moduleid].u<>hp) then
  774. internalerror(200501156);
  775. end
  776. else
  777. begin
  778. unitmap[hp.moduleid].u:=hp;
  779. unitmap[hp.moduleid].derefidx:=-1;
  780. end;
  781. inc(i);
  782. hp:=tmodule(hp.next);
  783. end;
  784. end;
  785. procedure tmodule.check_hints;
  786. begin
  787. if mo_hint_deprecated in moduleoptions then
  788. if (mo_has_deprecated_msg in moduleoptions) and (deprecatedmsg <> nil) then
  789. Message2(sym_w_deprecated_unit_with_msg,realmodulename^,deprecatedmsg^)
  790. else
  791. Message1(sym_w_deprecated_unit,realmodulename^);
  792. if mo_hint_experimental in moduleoptions then
  793. Message1(sym_w_experimental_unit,realmodulename^);
  794. if mo_hint_platform in moduleoptions then
  795. Message1(sym_w_non_portable_unit,realmodulename^);
  796. if mo_hint_library in moduleoptions then
  797. Message1(sym_w_library_unit,realmodulename^);
  798. if mo_hint_unimplemented in moduleoptions then
  799. Message1(sym_w_non_implemented_unit,realmodulename^);
  800. end;
  801. function tmodule.derefidx_unit(id:longint):longint;
  802. begin
  803. if id>=unitmapsize then
  804. internalerror(2005011511);
  805. if unitmap[id].derefidx=-1 then
  806. begin
  807. unitmap[id].derefidx:=derefmapcnt;
  808. inc(derefmapcnt);
  809. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  810. end;
  811. if unitmap[id].derefidx>=derefmapsize then
  812. internalerror(2005011514);
  813. result:=unitmap[id].derefidx;
  814. end;
  815. function tmodule.resolve_unit(id:longint):tmodule;
  816. var
  817. hp : tmodule;
  818. begin
  819. if id>=derefmapsize then
  820. internalerror(200306231);
  821. result:=derefmap[id].u;
  822. if not assigned(result) then
  823. begin
  824. if not assigned(derefmap[id].modulename) or
  825. (derefmap[id].modulename^='') then
  826. internalerror(200501159);
  827. hp:=tmodule(loaded_units.first);
  828. while assigned(hp) do
  829. begin
  830. { only check for units. The main program is also
  831. as a unit in the loaded_units list. We simply need
  832. to ignore this entry (PFV) }
  833. if hp.is_unit and
  834. (hp.modulename^=derefmap[id].modulename^) then
  835. break;
  836. hp:=tmodule(hp.next);
  837. end;
  838. if not assigned(hp) then
  839. internalerror(2005011510);
  840. derefmap[id].u:=hp;
  841. result:=hp;
  842. end;
  843. end;
  844. procedure tmodule.allunitsused;
  845. var
  846. pu : tused_unit;
  847. begin
  848. pu:=tused_unit(used_units.first);
  849. while assigned(pu) do
  850. begin
  851. if assigned(pu.u.globalsymtable) then
  852. begin
  853. if unitmap[pu.u.moduleid].u<>pu.u then
  854. internalerror(200501157);
  855. { Give a note when the unit is not referenced, skip
  856. this is for units with an initialization/finalization }
  857. if (unitmap[pu.u.moduleid].refs=0) and
  858. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  859. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  860. end;
  861. pu:=tused_unit(pu.next);
  862. end;
  863. end;
  864. procedure tmodule.setmodulename(const s:string);
  865. begin
  866. stringdispose(modulename);
  867. stringdispose(realmodulename);
  868. modulename:=stringdup(upper(s));
  869. realmodulename:=stringdup(s);
  870. { also update asmlibrary names }
  871. current_asmdata.name:=modulename^;
  872. current_asmdata.realname:=realmodulename^;
  873. end;
  874. procedure TModule.AddExternalImport(const libname,symname,symmangledname:string;
  875. OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  876. var
  877. ImportLibrary,OtherIL : TImportLibrary;
  878. ImportSymbol : TImportSymbol;
  879. i : longint;
  880. begin
  881. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  882. if not assigned(ImportLibrary) then
  883. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  884. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  885. if not assigned(ImportSymbol) then
  886. begin
  887. { Check that the same name does not exist in another library }
  888. { If it does and the same mangled name is used, issue a warning }
  889. if ImportLibraryList.Count>1 then
  890. for i:=0 To ImportLibraryList.Count-1 do
  891. begin
  892. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  893. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  894. if assigned(ImportSymbol) then
  895. begin
  896. if ImportSymbol.MangledName=symmangledname then
  897. begin
  898. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  899. break;
  900. end;
  901. end;
  902. end;
  903. if not ImportByOrdinalOnly then
  904. { negative ordinal number indicates import by name with ordinal number as hint }
  905. OrdNr:=-OrdNr;
  906. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  907. symname,symmangledname,OrdNr,isvar);
  908. end;
  909. end;
  910. initialization
  911. {$ifdef MEMDEBUG}
  912. memsymtable:=TMemDebug.create('Symtables');
  913. memsymtable.stop;
  914. {$endif MEMDEBUG}
  915. finalization
  916. {$ifdef MEMDEBUG}
  917. memsymtable.free;
  918. {$endif MEMDEBUG}
  919. end.