fmodule.pas 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. ptrdefs : THashSet; { list of pointerdefs created in this module so we can reuse them (not saved/restored) }
  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. namespace: pshortstring; { for JVM target: corresponds to Java package name }
  163. { for targets that initialise typed constants via explicit assignments
  164. instead of by generating an initialised data section (holds typed
  165. constant assignments at the module level; does not have to be saved
  166. into the ppu file, because translated into code during compilation)
  167. -- actual type: tnode (but fmodule should not depend on node) }
  168. tcinitcode : tobject;
  169. {create creates a new module which name is stored in 's'. LoadedFrom
  170. points to the module calling it. It is nil for the first compiled
  171. module. This allow inheritence of all path lists. MUST pay attention
  172. to that when creating link.res!!!!(mazen)}
  173. constructor create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  174. destructor destroy;override;
  175. procedure reset;virtual;
  176. procedure adddependency(callermodule:tmodule);
  177. procedure flagdependent(callermodule:tmodule);
  178. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  179. procedure updatemaps;
  180. procedure check_hints;
  181. function derefidx_unit(id:longint):longint;
  182. function resolve_unit(id:longint):tmodule;
  183. procedure allunitsused;
  184. procedure setmodulename(const s:string);
  185. procedure AddExternalImport(const libname,symname,symmangledname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  186. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  187. end;
  188. tused_unit = class(tlinkedlistitem)
  189. checksum,
  190. interface_checksum,
  191. indirect_checksum: cardinal;
  192. in_uses,
  193. in_interface : boolean;
  194. u : tmodule;
  195. unitsym : tunitsym;
  196. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  197. end;
  198. tdependent_unit = class(tlinkedlistitem)
  199. u : tmodule;
  200. constructor create(_u : tmodule);
  201. end;
  202. var
  203. main_module : tmodule; { Main module of the program }
  204. current_module : tmodule; { Current module which is compiled or loaded }
  205. compiled_module : tmodule; { Current module which is compiled }
  206. usedunits : tlinkedlist; { Used units for this program }
  207. loaded_units : tlinkedlist; { All loaded units }
  208. unloaded_units : tlinkedlist; { Units removed from loaded_units, to be freed }
  209. SmartLinkOFiles : TCmdStrList; { List of .o files which are generated,
  210. used to delete them after linking }
  211. procedure set_current_module(p:tmodule);
  212. function get_module(moduleindex : longint) : tmodule;
  213. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  214. procedure addloadedunit(hp:tmodule);
  215. function find_module_from_symtable(st:tsymtable):tmodule;
  216. implementation
  217. uses
  218. SysUtils,globals,
  219. verbose,systems,
  220. scanner,ppu,dbgbase,
  221. procinfo,symdef;
  222. {$ifdef MEMDEBUG}
  223. var
  224. memsymtable : TMemDebug;
  225. {$endif}
  226. {*****************************************************************************
  227. Global Functions
  228. *****************************************************************************}
  229. function find_module_from_symtable(st:tsymtable):tmodule;
  230. var
  231. hp : tmodule;
  232. begin
  233. result:=nil;
  234. hp:=tmodule(loaded_units.first);
  235. while assigned(hp) do
  236. begin
  237. if (hp.moduleid=st.moduleid) then
  238. begin
  239. result:=hp;
  240. exit;
  241. end;
  242. hp:=tmodule(hp.next);
  243. end;
  244. end;
  245. procedure set_current_module(p:tmodule);
  246. begin
  247. { save the state of the scanner }
  248. if assigned(current_scanner) then
  249. current_scanner.tempcloseinputfile;
  250. { set new module }
  251. current_module:=p;
  252. { restore previous module settings }
  253. Fillchar(current_filepos,0,sizeof(current_filepos));
  254. if assigned(current_module) then
  255. begin
  256. current_asmdata:=tasmdata(current_module.asmdata);
  257. current_debuginfo:=tdebuginfo(current_module.debuginfo);
  258. { restore scanner and file positions }
  259. current_scanner:=tscannerfile(current_module.scanner);
  260. if assigned(current_scanner) then
  261. begin
  262. current_scanner.tempopeninputfile;
  263. current_scanner.gettokenpos;
  264. parser_current_file:=current_scanner.inputfile.name^;
  265. end
  266. else
  267. begin
  268. current_filepos.moduleindex:=current_module.unit_index;
  269. parser_current_file:='';
  270. end;
  271. end
  272. else
  273. begin
  274. current_asmdata:=nil;
  275. current_scanner:=nil;
  276. current_debuginfo:=nil;
  277. end;
  278. end;
  279. function get_module(moduleindex : longint) : tmodule;
  280. var
  281. hp : tmodule;
  282. begin
  283. result:=nil;
  284. if moduleindex=0 then
  285. exit;
  286. result:=current_module;
  287. if not(assigned(loaded_units)) then
  288. exit;
  289. hp:=tmodule(loaded_units.first);
  290. while assigned(hp) and (hp.unit_index<>moduleindex) do
  291. hp:=tmodule(hp.next);
  292. result:=hp;
  293. end;
  294. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  295. var
  296. hp : tmodule;
  297. begin
  298. hp:=get_module(moduleindex);
  299. if assigned(hp) then
  300. get_source_file:=hp.sourcefiles.get_file(fileindex)
  301. else
  302. get_source_file:=nil;
  303. end;
  304. procedure addloadedunit(hp:tmodule);
  305. begin
  306. hp.moduleid:=loaded_units.count;
  307. loaded_units.concat(hp);
  308. end;
  309. {****************************************************************************
  310. TLinkContainerItem
  311. ****************************************************************************}
  312. constructor TLinkContainerItem.Create(const s:string;m:cardinal);
  313. begin
  314. inherited Create;
  315. data:=stringdup(s);
  316. needlink:=m;
  317. end;
  318. destructor TLinkContainerItem.Destroy;
  319. begin
  320. stringdispose(data);
  321. end;
  322. {****************************************************************************
  323. TLinkContainer
  324. ****************************************************************************}
  325. procedure TLinkContainer.add(const s : string;m:cardinal);
  326. begin
  327. inherited concat(TLinkContainerItem.Create(s,m));
  328. end;
  329. function TLinkContainer.get(var m:cardinal) : string;
  330. var
  331. p : tlinkcontaineritem;
  332. begin
  333. p:=tlinkcontaineritem(inherited getfirst);
  334. if p=nil then
  335. begin
  336. get:='';
  337. m:=0;
  338. end
  339. else
  340. begin
  341. get:=p.data^;
  342. m:=p.needlink;
  343. p.free;
  344. end;
  345. end;
  346. function TLinkContainer.getusemask(mask:cardinal) : string;
  347. var
  348. p : tlinkcontaineritem;
  349. found : boolean;
  350. begin
  351. found:=false;
  352. repeat
  353. p:=tlinkcontaineritem(inherited getfirst);
  354. if p=nil then
  355. begin
  356. getusemask:='';
  357. exit;
  358. end;
  359. getusemask:=p.data^;
  360. found:=(p.needlink and mask)<>0;
  361. p.free;
  362. until found;
  363. end;
  364. function TLinkContainer.find(const s:string):boolean;
  365. var
  366. newnode : tlinkcontaineritem;
  367. begin
  368. find:=false;
  369. newnode:=tlinkcontaineritem(First);
  370. while assigned(newnode) do
  371. begin
  372. if newnode.data^=s then
  373. begin
  374. find:=true;
  375. exit;
  376. end;
  377. newnode:=tlinkcontaineritem(newnode.next);
  378. end;
  379. end;
  380. {****************************************************************************
  381. TUSED_UNIT
  382. ****************************************************************************}
  383. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  384. begin
  385. u:=_u;
  386. in_interface:=intface;
  387. in_uses:=inuses;
  388. unitsym:=usym;
  389. if _u.state=ms_compiled then
  390. begin
  391. checksum:=u.crc;
  392. interface_checksum:=u.interface_crc;
  393. indirect_checksum:=u.indirect_crc;
  394. end
  395. else
  396. begin
  397. checksum:=0;
  398. interface_checksum:=0;
  399. indirect_checksum:=0;
  400. end;
  401. end;
  402. {****************************************************************************
  403. TDENPENDENT_UNIT
  404. ****************************************************************************}
  405. constructor tdependent_unit.create(_u : tmodule);
  406. begin
  407. u:=_u;
  408. end;
  409. {****************************************************************************
  410. TMODULE
  411. ****************************************************************************}
  412. constructor tmodule.create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  413. var
  414. n : string;
  415. begin
  416. n:=ChangeFileExt(ExtractFileName(s),'');
  417. { Programs have the name 'Program' to don't conflict with dup id's }
  418. if _is_unit then
  419. inherited create(n)
  420. else
  421. inherited create('Program');
  422. mainsource:=stringdup(s);
  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(s,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. ptrdefs:=THashSet.Create(64,true,false);
  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. namespace:=nil;
  487. tcinitcode:=nil;
  488. _exports:=TLinkedList.Create;
  489. dllscannerinputlist:=TFPHashList.Create;
  490. asmdata:=TAsmData.create(realmodulename^);
  491. InitDebugInfo(self);
  492. end;
  493. destructor tmodule.Destroy;
  494. var
  495. i : longint;
  496. begin
  497. if assigned(unitmap) then
  498. freemem(unitmap);
  499. if assigned(derefmap) then
  500. begin
  501. for i:=0 to derefmapcnt-1 do
  502. stringdispose(derefmap[i].modulename);
  503. freemem(derefmap);
  504. end;
  505. if assigned(_exports) then
  506. _exports.free;
  507. if assigned(dllscannerinputlist) then
  508. dllscannerinputlist.free;
  509. if assigned(scanner) then
  510. begin
  511. { also update current_scanner if it was pointing
  512. to this module }
  513. if current_scanner=tscannerfile(scanner) then
  514. current_scanner:=nil;
  515. tscannerfile(scanner).free;
  516. end;
  517. if assigned(asmdata) then
  518. begin
  519. if current_asmdata=asmdata then
  520. current_asmdata:=nil;
  521. asmdata.free;
  522. end;
  523. if assigned(procinfo) then
  524. begin
  525. if current_procinfo=tprocinfo(procinfo) then
  526. begin
  527. current_procinfo:=nil;
  528. current_structdef:=nil;
  529. current_genericdef:=nil;
  530. current_specializedef:=nil;
  531. end;
  532. { release procinfo tree }
  533. tprocinfo(procinfo).destroy_tree;
  534. end;
  535. DoneDebugInfo(self);
  536. used_units.free;
  537. dependent_units.free;
  538. resourcefiles.Free;
  539. linkunitofiles.Free;
  540. linkunitstaticlibs.Free;
  541. linkunitsharedlibs.Free;
  542. linkotherofiles.Free;
  543. linkotherstaticlibs.Free;
  544. linkothersharedlibs.Free;
  545. linkotherframeworks.Free;
  546. stringdispose(mainname);
  547. FImportLibraryList.Free;
  548. extendeddefs.Free;
  549. stringdispose(objfilename);
  550. stringdispose(asmfilename);
  551. stringdispose(ppufilename);
  552. stringdispose(importlibfilename);
  553. stringdispose(staticlibfilename);
  554. stringdispose(sharedlibfilename);
  555. stringdispose(exefilename);
  556. stringdispose(outputpath);
  557. stringdispose(path);
  558. stringdispose(realmodulename);
  559. stringdispose(mainsource);
  560. stringdispose(asmprefix);
  561. stringdispose(deprecatedmsg);
  562. stringdispose(namespace);
  563. tcinitcode.free;
  564. localunitsearchpath.Free;
  565. localobjectsearchpath.free;
  566. localincludesearchpath.free;
  567. locallibrarysearchpath.free;
  568. localframeworksearchpath.free;
  569. {$ifdef MEMDEBUG}
  570. memsymtable.start;
  571. {$endif}
  572. derefdata.free;
  573. deflist.free;
  574. symlist.free;
  575. ptrdefs.free;
  576. wpoinfo.free;
  577. checkforwarddefs.free;
  578. globalsymtable.free;
  579. localsymtable.free;
  580. globalmacrosymtable.free;
  581. localmacrosymtable.free;
  582. {$ifdef MEMDEBUG}
  583. memsymtable.stop;
  584. {$endif}
  585. stringdispose(modulename);
  586. inherited Destroy;
  587. end;
  588. procedure tmodule.reset;
  589. var
  590. i : longint;
  591. begin
  592. if assigned(scanner) then
  593. begin
  594. { also update current_scanner if it was pointing
  595. to this module }
  596. if current_scanner=tscannerfile(scanner) then
  597. current_scanner:=nil;
  598. tscannerfile(scanner).free;
  599. scanner:=nil;
  600. end;
  601. if assigned(procinfo) then
  602. begin
  603. if current_procinfo=tprocinfo(procinfo) then
  604. begin
  605. current_procinfo:=nil;
  606. current_structdef:=nil;
  607. current_genericdef:=nil;
  608. current_specializedef:=nil;
  609. end;
  610. { release procinfo tree }
  611. tprocinfo(procinfo).destroy_tree;
  612. end;
  613. if assigned(asmdata) then
  614. begin
  615. if current_asmdata=TAsmData(asmdata) then
  616. current_asmdata:=nil;
  617. asmdata.free;
  618. asmdata:=nil;
  619. end;
  620. DoneDebugInfo(self);
  621. globalsymtable.free;
  622. globalsymtable:=nil;
  623. localsymtable.free;
  624. localsymtable:=nil;
  625. globalmacrosymtable.free;
  626. globalmacrosymtable:=nil;
  627. localmacrosymtable.free;
  628. localmacrosymtable:=nil;
  629. deflist.free;
  630. deflist:=TFPObjectList.Create(false);
  631. symlist.free;
  632. symlist:=TFPObjectList.Create(false);
  633. ptrdefs.free;
  634. ptrdefs:=THashSet.Create(64,true,false);
  635. wpoinfo.free;
  636. wpoinfo:=nil;
  637. checkforwarddefs.free;
  638. checkforwarddefs:=TFPObjectList.Create(false);
  639. derefdata.free;
  640. derefdata:=TDynamicArray.Create(1024);
  641. if assigned(unitmap) then
  642. begin
  643. freemem(unitmap);
  644. unitmap:=nil;
  645. end;
  646. if assigned(derefmap) then
  647. begin
  648. for i:=0 to derefmapcnt-1 do
  649. stringdispose(derefmap[i].modulename);
  650. freemem(derefmap);
  651. derefmap:=nil;
  652. end;
  653. unitmapsize:=0;
  654. derefmapsize:=0;
  655. derefmapcnt:=0;
  656. derefdataintflen:=0;
  657. sourcefiles.free;
  658. sourcefiles:=tinputfilemanager.create;
  659. asmdata:=TAsmData.create(realmodulename^);
  660. InitDebugInfo(self);
  661. _exports.free;
  662. _exports:=tlinkedlist.create;
  663. dllscannerinputlist.free;
  664. dllscannerinputlist:=TFPHashList.create;
  665. used_units.free;
  666. used_units:=TLinkedList.Create;
  667. dependent_units.free;
  668. dependent_units:=TLinkedList.Create;
  669. resourcefiles.Free;
  670. resourcefiles:=TCmdStrList.Create;
  671. linkunitofiles.Free;
  672. linkunitofiles:=TLinkContainer.Create;
  673. linkunitstaticlibs.Free;
  674. linkunitstaticlibs:=TLinkContainer.Create;
  675. linkunitsharedlibs.Free;
  676. linkunitsharedlibs:=TLinkContainer.Create;
  677. linkotherofiles.Free;
  678. linkotherofiles:=TLinkContainer.Create;
  679. linkotherstaticlibs.Free;
  680. linkotherstaticlibs:=TLinkContainer.Create;
  681. linkothersharedlibs.Free;
  682. linkothersharedlibs:=TLinkContainer.Create;
  683. linkotherframeworks.Free;
  684. linkotherframeworks:=TLinkContainer.Create;
  685. stringdispose(mainname);
  686. FImportLibraryList.Free;
  687. FImportLibraryList:=TFPHashObjectList.Create;
  688. do_compile:=false;
  689. do_reload:=false;
  690. interface_compiled:=false;
  691. in_interface:=true;
  692. in_global:=true;
  693. mode_switch_allowed:=true;
  694. stringdispose(deprecatedmsg);
  695. stringdispose(namespace);
  696. tcinitcode.free;
  697. tcinitcode:=nil;
  698. moduleoptions:=[];
  699. is_dbginfo_written:=false;
  700. crc:=0;
  701. interface_crc:=0;
  702. indirect_crc:=0;
  703. flags:=0;
  704. mainfilepos.line:=0;
  705. mainfilepos.column:=0;
  706. mainfilepos.fileindex:=0;
  707. recompile_reason:=rr_unknown;
  708. {
  709. The following fields should not
  710. be reset:
  711. mainsource
  712. state
  713. loaded_from
  714. sources_avail
  715. }
  716. end;
  717. procedure tmodule.adddependency(callermodule:tmodule);
  718. begin
  719. { This is not needed for programs }
  720. if not callermodule.is_unit then
  721. exit;
  722. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  723. dependent_units.concat(tdependent_unit.create(callermodule));
  724. end;
  725. procedure tmodule.flagdependent(callermodule:tmodule);
  726. var
  727. pm : tdependent_unit;
  728. begin
  729. { flag all units that depend on this unit for reloading }
  730. pm:=tdependent_unit(current_module.dependent_units.first);
  731. while assigned(pm) do
  732. begin
  733. { We do not have to reload the unit that wants to load
  734. this unit, unless this unit is already compiled during
  735. the loading }
  736. if (pm.u=callermodule) and
  737. (pm.u.state<>ms_compiled) then
  738. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  739. else
  740. if pm.u.state=ms_second_compile then
  741. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  742. else
  743. begin
  744. pm.u.do_reload:=true;
  745. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  746. end;
  747. pm:=tdependent_unit(pm.next);
  748. end;
  749. end;
  750. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  751. var
  752. pu : tused_unit;
  753. begin
  754. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  755. used_units.concat(pu);
  756. addusedunit:=pu;
  757. end;
  758. procedure tmodule.updatemaps;
  759. var
  760. oldmapsize : longint;
  761. hp : tmodule;
  762. i : longint;
  763. begin
  764. { Extend unitmap }
  765. oldmapsize:=unitmapsize;
  766. unitmapsize:=loaded_units.count;
  767. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  768. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  769. { Extend Derefmap }
  770. oldmapsize:=derefmapsize;
  771. derefmapsize:=loaded_units.count;
  772. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  773. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  774. { Add all units to unitmap }
  775. hp:=tmodule(loaded_units.first);
  776. i:=0;
  777. while assigned(hp) do
  778. begin
  779. if hp.moduleid>=unitmapsize then
  780. internalerror(200501151);
  781. { Verify old entries }
  782. if (i<oldmapsize) then
  783. begin
  784. if (hp.moduleid<>i) or
  785. (unitmap[hp.moduleid].u<>hp) then
  786. internalerror(200501156);
  787. end
  788. else
  789. begin
  790. unitmap[hp.moduleid].u:=hp;
  791. unitmap[hp.moduleid].derefidx:=-1;
  792. end;
  793. inc(i);
  794. hp:=tmodule(hp.next);
  795. end;
  796. end;
  797. procedure tmodule.check_hints;
  798. begin
  799. if mo_hint_deprecated in moduleoptions then
  800. if (mo_has_deprecated_msg in moduleoptions) and (deprecatedmsg <> nil) then
  801. Message2(sym_w_deprecated_unit_with_msg,realmodulename^,deprecatedmsg^)
  802. else
  803. Message1(sym_w_deprecated_unit,realmodulename^);
  804. if mo_hint_experimental in moduleoptions then
  805. Message1(sym_w_experimental_unit,realmodulename^);
  806. if mo_hint_platform in moduleoptions then
  807. Message1(sym_w_non_portable_unit,realmodulename^);
  808. if mo_hint_library in moduleoptions then
  809. Message1(sym_w_library_unit,realmodulename^);
  810. if mo_hint_unimplemented in moduleoptions then
  811. Message1(sym_w_non_implemented_unit,realmodulename^);
  812. end;
  813. function tmodule.derefidx_unit(id:longint):longint;
  814. begin
  815. if id>=unitmapsize then
  816. internalerror(2005011511);
  817. if unitmap[id].derefidx=-1 then
  818. begin
  819. unitmap[id].derefidx:=derefmapcnt;
  820. inc(derefmapcnt);
  821. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  822. end;
  823. if unitmap[id].derefidx>=derefmapsize then
  824. internalerror(2005011514);
  825. result:=unitmap[id].derefidx;
  826. end;
  827. function tmodule.resolve_unit(id:longint):tmodule;
  828. var
  829. hp : tmodule;
  830. begin
  831. if id>=derefmapsize then
  832. internalerror(200306231);
  833. result:=derefmap[id].u;
  834. if not assigned(result) then
  835. begin
  836. if not assigned(derefmap[id].modulename) or
  837. (derefmap[id].modulename^='') then
  838. internalerror(200501159);
  839. hp:=tmodule(loaded_units.first);
  840. while assigned(hp) do
  841. begin
  842. { only check for units. The main program is also
  843. as a unit in the loaded_units list. We simply need
  844. to ignore this entry (PFV) }
  845. if hp.is_unit and
  846. (hp.modulename^=derefmap[id].modulename^) then
  847. break;
  848. hp:=tmodule(hp.next);
  849. end;
  850. if not assigned(hp) then
  851. internalerror(2005011510);
  852. derefmap[id].u:=hp;
  853. result:=hp;
  854. end;
  855. end;
  856. procedure tmodule.allunitsused;
  857. var
  858. pu : tused_unit;
  859. begin
  860. pu:=tused_unit(used_units.first);
  861. while assigned(pu) do
  862. begin
  863. if assigned(pu.u.globalsymtable) then
  864. begin
  865. if unitmap[pu.u.moduleid].u<>pu.u then
  866. internalerror(200501157);
  867. { Give a note when the unit is not referenced, skip
  868. this is for units with an initialization/finalization }
  869. if (unitmap[pu.u.moduleid].refs=0) and
  870. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  871. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  872. end;
  873. pu:=tused_unit(pu.next);
  874. end;
  875. end;
  876. procedure tmodule.setmodulename(const s:string);
  877. begin
  878. stringdispose(modulename);
  879. stringdispose(realmodulename);
  880. modulename:=stringdup(upper(s));
  881. realmodulename:=stringdup(s);
  882. { also update asmlibrary names }
  883. current_asmdata.name:=modulename^;
  884. current_asmdata.realname:=realmodulename^;
  885. end;
  886. procedure TModule.AddExternalImport(const libname,symname,symmangledname:string;
  887. OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  888. var
  889. ImportLibrary,OtherIL : TImportLibrary;
  890. ImportSymbol : TImportSymbol;
  891. i : longint;
  892. begin
  893. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  894. if not assigned(ImportLibrary) then
  895. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  896. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  897. if not assigned(ImportSymbol) then
  898. begin
  899. { Check that the same name does not exist in another library }
  900. { If it does and the same mangled name is used, issue a warning }
  901. if ImportLibraryList.Count>1 then
  902. for i:=0 To ImportLibraryList.Count-1 do
  903. begin
  904. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  905. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  906. if assigned(ImportSymbol) then
  907. begin
  908. if ImportSymbol.MangledName=symmangledname then
  909. begin
  910. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  911. break;
  912. end;
  913. end;
  914. end;
  915. if not ImportByOrdinalOnly then
  916. { negative ordinal number indicates import by name with ordinal number as hint }
  917. OrdNr:=-OrdNr;
  918. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  919. symname,symmangledname,OrdNr,isvar);
  920. end;
  921. end;
  922. initialization
  923. {$ifdef MEMDEBUG}
  924. memsymtable:=TMemDebug.create('Symtables');
  925. memsymtable.stop;
  926. {$endif MEMDEBUG}
  927. finalization
  928. {$ifdef MEMDEBUG}
  929. memsymtable.free;
  930. {$endif MEMDEBUG}
  931. end.