fmodule.pas 33 KB

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