fmodule.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the first loading and searching of the modules
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit fmodule;
  18. {$i fpcdefs.inc}
  19. {$ifdef go32v2}
  20. {$define shortasmprefix}
  21. {$endif}
  22. {$ifdef watcom}
  23. {$define shortasmprefix}
  24. {$endif}
  25. {$ifdef tos}
  26. {$define shortasmprefix}
  27. {$endif}
  28. {$ifdef OS2}
  29. { Allthough OS/2 supports long filenames I play it safe and
  30. use 8.3 filenames, because this allows the compiler to run
  31. on a FAT partition. (DM) }
  32. {$define shortasmprefix}
  33. {$endif}
  34. interface
  35. uses
  36. cutils,cclasses,cfileutl,
  37. globtype,finput,ogbase,
  38. symbase,symsym,
  39. wpobase,
  40. aasmbase,aasmtai,aasmdata;
  41. const
  42. UNSPECIFIED_LIBRARY_NAME = '<none>';
  43. type
  44. trecompile_reason = (rr_unknown,
  45. rr_noppu,rr_sourcenewer,rr_build,rr_crcchanged
  46. );
  47. { unit options }
  48. tmoduleoption = (mo_none,
  49. mo_hint_deprecated,
  50. mo_hint_platform,
  51. mo_hint_library,
  52. mo_hint_unimplemented,
  53. mo_hint_experimental,
  54. mo_has_deprecated_msg
  55. );
  56. tmoduleoptions = set of tmoduleoption;
  57. tlinkcontaineritem=class(tlinkedlistitem)
  58. public
  59. data : pshortstring;
  60. needlink : cardinal;
  61. constructor Create(const s:string;m:cardinal);
  62. destructor Destroy;override;
  63. end;
  64. tlinkcontainer=class(tlinkedlist)
  65. procedure add(const s : string;m:cardinal);
  66. function get(var m:cardinal) : string;
  67. function getusemask(mask:cardinal) : string;
  68. function find(const s:string):boolean;
  69. end;
  70. tmodule = class;
  71. tused_unit = class;
  72. tunitmaprec = record
  73. u : tmodule;
  74. { number of references }
  75. refs : longint;
  76. { index in the derefmap }
  77. derefidx : longint;
  78. end;
  79. punitmap = ^tunitmaprec;
  80. tderefmaprec = record
  81. u : tmodule;
  82. { modulename, used during ppu load }
  83. modulename : pshortstring;
  84. end;
  85. pderefmap = ^tderefmaprec;
  86. { tmodule }
  87. tmodule = class(tmodulebase)
  88. private
  89. FImportLibraryList : TFPHashObjectList;
  90. public
  91. do_reload, { force reloading of the unit }
  92. do_compile, { need to compile the sources }
  93. sources_avail, { if all sources are reachable }
  94. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  95. is_dbginfo_written,
  96. is_unit,
  97. in_interface, { processing the implementation part? }
  98. { allow global settings }
  99. in_global : boolean;
  100. { Whether a mode switch is still allowed at this point in the parsing.}
  101. mode_switch_allowed,
  102. { generate pic helper which loads eip in ecx (for leave procedures) }
  103. requires_ecx_pic_helper,
  104. { generate pic helper which loads eip in ebx (for non leave procedures) }
  105. requires_ebx_pic_helper : boolean;
  106. interface_only: boolean; { interface-only macpas unit; flag does not need saving/restoring to ppu }
  107. mainfilepos : tfileposinfo;
  108. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  109. crc,
  110. interface_crc,
  111. indirect_crc : cardinal;
  112. flags : cardinal; { the PPU flags }
  113. islibrary : boolean; { if it is a library (win32 dll) }
  114. IsPackage : boolean;
  115. moduleid : longint;
  116. unitmap : punitmap; { mapping of all used units }
  117. unitmapsize : longint; { number of units in the map }
  118. derefmap : pderefmap; { mapping of all units needed for deref }
  119. derefmapcnt : longint; { number of units in the map }
  120. derefmapsize : longint; { number of units in the map }
  121. derefdataintflen : longint;
  122. derefdata : tdynamicarray;
  123. checkforwarddefs,
  124. deflist,
  125. symlist : TFPObjectList;
  126. wpoinfo : tunitwpoinfobase; { whole program optimization-related information that is generated during the current run for this unit }
  127. globalsymtable, { pointer to the global symtable of this unit }
  128. localsymtable : TSymtable;{ pointer to the local symtable of this unit }
  129. globalmacrosymtable, { pointer to the global macro symtable of this unit }
  130. localmacrosymtable : TSymtable;{ pointer to the local macro symtable of this unit }
  131. scanner : TObject; { scanner object used }
  132. procinfo : TObject; { current procedure being compiled }
  133. asmdata : TObject; { Assembler data }
  134. asmprefix : pshortstring; { prefix for the smartlink asmfiles }
  135. debuginfo : TObject;
  136. loaded_from : tmodule;
  137. _exports : tlinkedlist;
  138. dllscannerinputlist : TFPHashList;
  139. resourcefiles : TCmdStrList;
  140. linkunitofiles,
  141. linkunitstaticlibs,
  142. linkunitsharedlibs,
  143. linkotherofiles, { objects,libs loaded from the source }
  144. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  145. linkotherstaticlibs,
  146. linkotherframeworks : tlinkcontainer;
  147. mainname : pshortstring; { alternate name for "main" procedure }
  148. used_units : tlinkedlist;
  149. dependent_units : tlinkedlist;
  150. localunitsearchpath, { local searchpaths }
  151. localobjectsearchpath,
  152. localincludesearchpath,
  153. locallibrarysearchpath,
  154. localframeworksearchpath : TSearchPathList;
  155. moduleoptions: tmoduleoptions;
  156. deprecatedmsg: pshortstring;
  157. { contains a list of types that are extended by helper types; the key is
  158. the full name of the type and the data is a TFPObjectList of
  159. tobjectdef instances (the helper defs) }
  160. extendeddefs: TFPHashObjectList;
  161. namespace: pshortstring; { for JVM target: corresponds to Java package name }
  162. { for targets that initialise typed constants via explicit assignments
  163. instead of by generating an initialised data section (holds typed
  164. constant assignments at the module level; does not have to be saved
  165. into the ppu file, because translated into code during compilation)
  166. -- actual type: tnode (but fmodule should not depend on node) }
  167. tcinitcode : tobject;
  168. {create creates a new module which name is stored in 's'. LoadedFrom
  169. points to the module calling it. It is nil for the first compiled
  170. module. This allow inheritence of all path lists. MUST pay attention
  171. to that when creating link.res!!!!(mazen)}
  172. constructor create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  173. destructor destroy;override;
  174. procedure reset;virtual;
  175. procedure adddependency(callermodule:tmodule);
  176. procedure flagdependent(callermodule:tmodule);
  177. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  178. procedure updatemaps;
  179. procedure check_hints;
  180. function derefidx_unit(id:longint):longint;
  181. function resolve_unit(id:longint):tmodule;
  182. procedure allunitsused;
  183. procedure setmodulename(const s:string);
  184. procedure AddExternalImport(const libname,symname,symmangledname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  185. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  186. end;
  187. tused_unit = class(tlinkedlistitem)
  188. checksum,
  189. interface_checksum,
  190. indirect_checksum: cardinal;
  191. in_uses,
  192. in_interface : boolean;
  193. u : tmodule;
  194. unitsym : tunitsym;
  195. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  196. end;
  197. tdependent_unit = class(tlinkedlistitem)
  198. u : tmodule;
  199. constructor create(_u : tmodule);
  200. end;
  201. var
  202. main_module : tmodule; { Main module of the program }
  203. current_module : tmodule; { Current module which is compiled or loaded }
  204. compiled_module : tmodule; { Current module which is compiled }
  205. usedunits : tlinkedlist; { Used units for this program }
  206. loaded_units : tlinkedlist; { All loaded units }
  207. unloaded_units : tlinkedlist; { Units removed from loaded_units, to be freed }
  208. SmartLinkOFiles : TCmdStrList; { List of .o files which are generated,
  209. used to delete them after linking }
  210. procedure set_current_module(p:tmodule);
  211. function get_module(moduleindex : longint) : tmodule;
  212. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  213. procedure addloadedunit(hp:tmodule);
  214. function find_module_from_symtable(st:tsymtable):tmodule;
  215. implementation
  216. uses
  217. SysUtils,globals,
  218. verbose,systems,
  219. scanner,ppu,dbgbase,
  220. procinfo,symdef;
  221. {$ifdef MEMDEBUG}
  222. var
  223. memsymtable : TMemDebug;
  224. {$endif}
  225. {*****************************************************************************
  226. Global Functions
  227. *****************************************************************************}
  228. function find_module_from_symtable(st:tsymtable):tmodule;
  229. var
  230. hp : tmodule;
  231. begin
  232. result:=nil;
  233. hp:=tmodule(loaded_units.first);
  234. while assigned(hp) do
  235. begin
  236. if (hp.moduleid=st.moduleid) then
  237. begin
  238. result:=hp;
  239. exit;
  240. end;
  241. hp:=tmodule(hp.next);
  242. end;
  243. end;
  244. procedure set_current_module(p:tmodule);
  245. begin
  246. { save the state of the scanner }
  247. if assigned(current_scanner) then
  248. current_scanner.tempcloseinputfile;
  249. { set new module }
  250. current_module:=p;
  251. { restore previous module settings }
  252. Fillchar(current_filepos,0,sizeof(current_filepos));
  253. if assigned(current_module) then
  254. begin
  255. current_asmdata:=tasmdata(current_module.asmdata);
  256. current_debuginfo:=tdebuginfo(current_module.debuginfo);
  257. { restore scanner and file positions }
  258. current_scanner:=tscannerfile(current_module.scanner);
  259. if assigned(current_scanner) then
  260. begin
  261. current_scanner.tempopeninputfile;
  262. current_scanner.gettokenpos;
  263. parser_current_file:=current_scanner.inputfile.name^;
  264. end
  265. else
  266. begin
  267. current_filepos.moduleindex:=current_module.unit_index;
  268. parser_current_file:='';
  269. end;
  270. end
  271. else
  272. begin
  273. current_asmdata:=nil;
  274. current_scanner:=nil;
  275. current_debuginfo:=nil;
  276. end;
  277. end;
  278. function get_module(moduleindex : longint) : tmodule;
  279. var
  280. hp : tmodule;
  281. begin
  282. result:=nil;
  283. if moduleindex=0 then
  284. exit;
  285. result:=current_module;
  286. if not(assigned(loaded_units)) then
  287. exit;
  288. hp:=tmodule(loaded_units.first);
  289. while assigned(hp) and (hp.unit_index<>moduleindex) do
  290. hp:=tmodule(hp.next);
  291. result:=hp;
  292. end;
  293. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  294. var
  295. hp : tmodule;
  296. begin
  297. hp:=get_module(moduleindex);
  298. if assigned(hp) then
  299. get_source_file:=hp.sourcefiles.get_file(fileindex)
  300. else
  301. get_source_file:=nil;
  302. end;
  303. procedure addloadedunit(hp:tmodule);
  304. begin
  305. hp.moduleid:=loaded_units.count;
  306. loaded_units.concat(hp);
  307. end;
  308. {****************************************************************************
  309. TLinkContainerItem
  310. ****************************************************************************}
  311. constructor TLinkContainerItem.Create(const s:string;m:cardinal);
  312. begin
  313. inherited Create;
  314. data:=stringdup(s);
  315. needlink:=m;
  316. end;
  317. destructor TLinkContainerItem.Destroy;
  318. begin
  319. stringdispose(data);
  320. end;
  321. {****************************************************************************
  322. TLinkContainer
  323. ****************************************************************************}
  324. procedure TLinkContainer.add(const s : string;m:cardinal);
  325. begin
  326. inherited concat(TLinkContainerItem.Create(s,m));
  327. end;
  328. function TLinkContainer.get(var m:cardinal) : string;
  329. var
  330. p : tlinkcontaineritem;
  331. begin
  332. p:=tlinkcontaineritem(inherited getfirst);
  333. if p=nil then
  334. begin
  335. get:='';
  336. m:=0;
  337. end
  338. else
  339. begin
  340. get:=p.data^;
  341. m:=p.needlink;
  342. p.free;
  343. end;
  344. end;
  345. function TLinkContainer.getusemask(mask:cardinal) : string;
  346. var
  347. p : tlinkcontaineritem;
  348. found : boolean;
  349. begin
  350. found:=false;
  351. repeat
  352. p:=tlinkcontaineritem(inherited getfirst);
  353. if p=nil then
  354. begin
  355. getusemask:='';
  356. exit;
  357. end;
  358. getusemask:=p.data^;
  359. found:=(p.needlink and mask)<>0;
  360. p.free;
  361. until found;
  362. end;
  363. function TLinkContainer.find(const s:string):boolean;
  364. var
  365. newnode : tlinkcontaineritem;
  366. begin
  367. find:=false;
  368. newnode:=tlinkcontaineritem(First);
  369. while assigned(newnode) do
  370. begin
  371. if newnode.data^=s then
  372. begin
  373. find:=true;
  374. exit;
  375. end;
  376. newnode:=tlinkcontaineritem(newnode.next);
  377. end;
  378. end;
  379. {****************************************************************************
  380. TUSED_UNIT
  381. ****************************************************************************}
  382. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  383. begin
  384. u:=_u;
  385. in_interface:=intface;
  386. in_uses:=inuses;
  387. unitsym:=usym;
  388. if _u.state=ms_compiled then
  389. begin
  390. checksum:=u.crc;
  391. interface_checksum:=u.interface_crc;
  392. indirect_checksum:=u.indirect_crc;
  393. end
  394. else
  395. begin
  396. checksum:=0;
  397. interface_checksum:=0;
  398. indirect_checksum:=0;
  399. end;
  400. end;
  401. {****************************************************************************
  402. TDENPENDENT_UNIT
  403. ****************************************************************************}
  404. constructor tdependent_unit.create(_u : tmodule);
  405. begin
  406. u:=_u;
  407. end;
  408. {****************************************************************************
  409. TMODULE
  410. ****************************************************************************}
  411. constructor tmodule.create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  412. var
  413. n : string;
  414. begin
  415. n:=ChangeFileExt(ExtractFileName(s),'');
  416. { Programs have the name 'Program' to don't conflict with dup id's }
  417. if _is_unit then
  418. inherited create(n)
  419. else
  420. inherited create('Program');
  421. mainsource:=stringdup(s);
  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(s,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. namespace:=nil;
  485. tcinitcode:=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. stringdispose(namespace);
  561. tcinitcode.free;
  562. localunitsearchpath.Free;
  563. localobjectsearchpath.free;
  564. localincludesearchpath.free;
  565. locallibrarysearchpath.free;
  566. localframeworksearchpath.free;
  567. {$ifdef MEMDEBUG}
  568. memsymtable.start;
  569. {$endif}
  570. derefdata.free;
  571. deflist.free;
  572. symlist.free;
  573. wpoinfo.free;
  574. checkforwarddefs.free;
  575. globalsymtable.free;
  576. localsymtable.free;
  577. globalmacrosymtable.free;
  578. localmacrosymtable.free;
  579. {$ifdef MEMDEBUG}
  580. memsymtable.stop;
  581. {$endif}
  582. stringdispose(modulename);
  583. inherited Destroy;
  584. end;
  585. procedure tmodule.reset;
  586. var
  587. i : longint;
  588. begin
  589. if assigned(scanner) then
  590. begin
  591. { also update current_scanner if it was pointing
  592. to this module }
  593. if current_scanner=tscannerfile(scanner) then
  594. current_scanner:=nil;
  595. tscannerfile(scanner).free;
  596. scanner:=nil;
  597. end;
  598. if assigned(procinfo) then
  599. begin
  600. if current_procinfo=tprocinfo(procinfo) then
  601. begin
  602. current_procinfo:=nil;
  603. current_structdef:=nil;
  604. current_genericdef:=nil;
  605. current_specializedef:=nil;
  606. end;
  607. { release procinfo tree }
  608. tprocinfo(procinfo).destroy_tree;
  609. end;
  610. if assigned(asmdata) then
  611. begin
  612. if current_asmdata=TAsmData(asmdata) then
  613. current_asmdata:=nil;
  614. asmdata.free;
  615. asmdata:=nil;
  616. end;
  617. DoneDebugInfo(self);
  618. globalsymtable.free;
  619. globalsymtable:=nil;
  620. localsymtable.free;
  621. localsymtable:=nil;
  622. globalmacrosymtable.free;
  623. globalmacrosymtable:=nil;
  624. localmacrosymtable.free;
  625. localmacrosymtable:=nil;
  626. deflist.free;
  627. deflist:=TFPObjectList.Create(false);
  628. symlist.free;
  629. symlist:=TFPObjectList.Create(false);
  630. wpoinfo.free;
  631. wpoinfo:=nil;
  632. checkforwarddefs.free;
  633. checkforwarddefs:=TFPObjectList.Create(false);
  634. derefdata.free;
  635. derefdata:=TDynamicArray.Create(1024);
  636. if assigned(unitmap) then
  637. begin
  638. freemem(unitmap);
  639. unitmap:=nil;
  640. end;
  641. if assigned(derefmap) then
  642. begin
  643. for i:=0 to derefmapcnt-1 do
  644. stringdispose(derefmap[i].modulename);
  645. freemem(derefmap);
  646. derefmap:=nil;
  647. end;
  648. unitmapsize:=0;
  649. derefmapsize:=0;
  650. derefmapcnt:=0;
  651. derefdataintflen:=0;
  652. sourcefiles.free;
  653. sourcefiles:=tinputfilemanager.create;
  654. asmdata:=TAsmData.create(realmodulename^);
  655. InitDebugInfo(self);
  656. _exports.free;
  657. _exports:=tlinkedlist.create;
  658. dllscannerinputlist.free;
  659. dllscannerinputlist:=TFPHashList.create;
  660. used_units.free;
  661. used_units:=TLinkedList.Create;
  662. dependent_units.free;
  663. dependent_units:=TLinkedList.Create;
  664. resourcefiles.Free;
  665. resourcefiles:=TCmdStrList.Create;
  666. linkunitofiles.Free;
  667. linkunitofiles:=TLinkContainer.Create;
  668. linkunitstaticlibs.Free;
  669. linkunitstaticlibs:=TLinkContainer.Create;
  670. linkunitsharedlibs.Free;
  671. linkunitsharedlibs:=TLinkContainer.Create;
  672. linkotherofiles.Free;
  673. linkotherofiles:=TLinkContainer.Create;
  674. linkotherstaticlibs.Free;
  675. linkotherstaticlibs:=TLinkContainer.Create;
  676. linkothersharedlibs.Free;
  677. linkothersharedlibs:=TLinkContainer.Create;
  678. linkotherframeworks.Free;
  679. linkotherframeworks:=TLinkContainer.Create;
  680. stringdispose(mainname);
  681. FImportLibraryList.Free;
  682. FImportLibraryList:=TFPHashObjectList.Create;
  683. do_compile:=false;
  684. do_reload:=false;
  685. interface_compiled:=false;
  686. in_interface:=true;
  687. in_global:=true;
  688. mode_switch_allowed:=true;
  689. stringdispose(deprecatedmsg);
  690. stringdispose(namespace);
  691. tcinitcode.free;
  692. tcinitcode:=nil;
  693. moduleoptions:=[];
  694. is_dbginfo_written:=false;
  695. crc:=0;
  696. interface_crc:=0;
  697. indirect_crc:=0;
  698. flags:=0;
  699. mainfilepos.line:=0;
  700. mainfilepos.column:=0;
  701. mainfilepos.fileindex:=0;
  702. recompile_reason:=rr_unknown;
  703. {
  704. The following fields should not
  705. be reset:
  706. mainsource
  707. state
  708. loaded_from
  709. sources_avail
  710. }
  711. end;
  712. procedure tmodule.adddependency(callermodule:tmodule);
  713. begin
  714. { This is not needed for programs }
  715. if not callermodule.is_unit then
  716. exit;
  717. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  718. dependent_units.concat(tdependent_unit.create(callermodule));
  719. end;
  720. procedure tmodule.flagdependent(callermodule:tmodule);
  721. var
  722. pm : tdependent_unit;
  723. begin
  724. { flag all units that depend on this unit for reloading }
  725. pm:=tdependent_unit(current_module.dependent_units.first);
  726. while assigned(pm) do
  727. begin
  728. { We do not have to reload the unit that wants to load
  729. this unit, unless this unit is already compiled during
  730. the loading }
  731. if (pm.u=callermodule) and
  732. (pm.u.state<>ms_compiled) then
  733. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  734. else
  735. if pm.u.state=ms_second_compile then
  736. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  737. else
  738. begin
  739. pm.u.do_reload:=true;
  740. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  741. end;
  742. pm:=tdependent_unit(pm.next);
  743. end;
  744. end;
  745. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  746. var
  747. pu : tused_unit;
  748. begin
  749. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  750. used_units.concat(pu);
  751. addusedunit:=pu;
  752. end;
  753. procedure tmodule.updatemaps;
  754. var
  755. oldmapsize : longint;
  756. hp : tmodule;
  757. i : longint;
  758. begin
  759. { Extend unitmap }
  760. oldmapsize:=unitmapsize;
  761. unitmapsize:=loaded_units.count;
  762. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  763. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  764. { Extend Derefmap }
  765. oldmapsize:=derefmapsize;
  766. derefmapsize:=loaded_units.count;
  767. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  768. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  769. { Add all units to unitmap }
  770. hp:=tmodule(loaded_units.first);
  771. i:=0;
  772. while assigned(hp) do
  773. begin
  774. if hp.moduleid>=unitmapsize then
  775. internalerror(200501151);
  776. { Verify old entries }
  777. if (i<oldmapsize) then
  778. begin
  779. if (hp.moduleid<>i) or
  780. (unitmap[hp.moduleid].u<>hp) then
  781. internalerror(200501156);
  782. end
  783. else
  784. begin
  785. unitmap[hp.moduleid].u:=hp;
  786. unitmap[hp.moduleid].derefidx:=-1;
  787. end;
  788. inc(i);
  789. hp:=tmodule(hp.next);
  790. end;
  791. end;
  792. procedure tmodule.check_hints;
  793. begin
  794. if mo_hint_deprecated in moduleoptions then
  795. if (mo_has_deprecated_msg in moduleoptions) and (deprecatedmsg <> nil) then
  796. Message2(sym_w_deprecated_unit_with_msg,realmodulename^,deprecatedmsg^)
  797. else
  798. Message1(sym_w_deprecated_unit,realmodulename^);
  799. if mo_hint_experimental in moduleoptions then
  800. Message1(sym_w_experimental_unit,realmodulename^);
  801. if mo_hint_platform in moduleoptions then
  802. Message1(sym_w_non_portable_unit,realmodulename^);
  803. if mo_hint_library in moduleoptions then
  804. Message1(sym_w_library_unit,realmodulename^);
  805. if mo_hint_unimplemented in moduleoptions then
  806. Message1(sym_w_non_implemented_unit,realmodulename^);
  807. end;
  808. function tmodule.derefidx_unit(id:longint):longint;
  809. begin
  810. if id>=unitmapsize then
  811. internalerror(2005011511);
  812. if unitmap[id].derefidx=-1 then
  813. begin
  814. unitmap[id].derefidx:=derefmapcnt;
  815. inc(derefmapcnt);
  816. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  817. end;
  818. if unitmap[id].derefidx>=derefmapsize then
  819. internalerror(2005011514);
  820. result:=unitmap[id].derefidx;
  821. end;
  822. function tmodule.resolve_unit(id:longint):tmodule;
  823. var
  824. hp : tmodule;
  825. begin
  826. if id>=derefmapsize then
  827. internalerror(200306231);
  828. result:=derefmap[id].u;
  829. if not assigned(result) then
  830. begin
  831. if not assigned(derefmap[id].modulename) or
  832. (derefmap[id].modulename^='') then
  833. internalerror(200501159);
  834. hp:=tmodule(loaded_units.first);
  835. while assigned(hp) do
  836. begin
  837. { only check for units. The main program is also
  838. as a unit in the loaded_units list. We simply need
  839. to ignore this entry (PFV) }
  840. if hp.is_unit and
  841. (hp.modulename^=derefmap[id].modulename^) then
  842. break;
  843. hp:=tmodule(hp.next);
  844. end;
  845. if not assigned(hp) then
  846. internalerror(2005011510);
  847. derefmap[id].u:=hp;
  848. result:=hp;
  849. end;
  850. end;
  851. procedure tmodule.allunitsused;
  852. var
  853. pu : tused_unit;
  854. begin
  855. pu:=tused_unit(used_units.first);
  856. while assigned(pu) do
  857. begin
  858. if assigned(pu.u.globalsymtable) then
  859. begin
  860. if unitmap[pu.u.moduleid].u<>pu.u then
  861. internalerror(200501157);
  862. { Give a note when the unit is not referenced, skip
  863. this is for units with an initialization/finalization }
  864. if (unitmap[pu.u.moduleid].refs=0) and
  865. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  866. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  867. end;
  868. pu:=tused_unit(pu.next);
  869. end;
  870. end;
  871. procedure tmodule.setmodulename(const s:string);
  872. begin
  873. stringdispose(modulename);
  874. stringdispose(realmodulename);
  875. modulename:=stringdup(upper(s));
  876. realmodulename:=stringdup(s);
  877. { also update asmlibrary names }
  878. current_asmdata.name:=modulename^;
  879. current_asmdata.realname:=realmodulename^;
  880. end;
  881. procedure TModule.AddExternalImport(const libname,symname,symmangledname:string;
  882. OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  883. var
  884. ImportLibrary,OtherIL : TImportLibrary;
  885. ImportSymbol : TImportSymbol;
  886. i : longint;
  887. begin
  888. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  889. if not assigned(ImportLibrary) then
  890. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  891. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  892. if not assigned(ImportSymbol) then
  893. begin
  894. { Check that the same name does not exist in another library }
  895. { If it does and the same mangled name is used, issue a warning }
  896. if ImportLibraryList.Count>1 then
  897. for i:=0 To ImportLibraryList.Count-1 do
  898. begin
  899. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  900. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  901. if assigned(ImportSymbol) then
  902. begin
  903. if ImportSymbol.MangledName=symmangledname then
  904. begin
  905. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  906. break;
  907. end;
  908. end;
  909. end;
  910. if not ImportByOrdinalOnly then
  911. { negative ordinal number indicates import by name with ordinal number as hint }
  912. OrdNr:=-OrdNr;
  913. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  914. symname,symmangledname,OrdNr,isvar);
  915. end;
  916. end;
  917. initialization
  918. {$ifdef MEMDEBUG}
  919. memsymtable:=TMemDebug.create('Symtables');
  920. memsymtable.stop;
  921. {$endif MEMDEBUG}
  922. finalization
  923. {$ifdef MEMDEBUG}
  924. memsymtable.free;
  925. {$endif MEMDEBUG}
  926. end.