fmodule.pas 33 KB

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