fmodule.pas 33 KB

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