fmodule.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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,symconst,symsym,symcpu,
  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 : TPathStr;
  60. needlink : cardinal;
  61. constructor Create(const s:TPathStr;m:cardinal);
  62. end;
  63. tlinkcontainer=class(tlinkedlist)
  64. procedure add(const s : TPathStr;m:cardinal);
  65. function get(var m:cardinal) : TPathStr;
  66. function getusemask(mask:cardinal) : TPathStr;
  67. function find(const s:TPathStr):boolean;
  68. end;
  69. tmodule = class;
  70. tused_unit = class;
  71. tunitmaprec = record
  72. u : tmodule;
  73. { number of references }
  74. refs : longint;
  75. { index in the derefmap }
  76. derefidx : longint;
  77. end;
  78. punitmap = ^tunitmaprec;
  79. tderefmaprec = record
  80. u : tmodule;
  81. { modulename, used during ppu load }
  82. modulename : pshortstring;
  83. end;
  84. pderefmap = ^tderefmaprec;
  85. { tmodule }
  86. tmodule = class(tmodulebase)
  87. private
  88. FImportLibraryList : TFPHashObjectList;
  89. public
  90. do_reload, { force reloading of the unit }
  91. do_compile, { need to compile the sources }
  92. sources_avail, { if all sources are reachable }
  93. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  94. is_dbginfo_written,
  95. is_unit,
  96. in_interface, { processing the implementation part? }
  97. { allow global settings }
  98. in_global : boolean;
  99. { Whether a mode switch is still allowed at this point in the parsing.}
  100. mode_switch_allowed,
  101. { generate pic helper which loads eip in ecx (for leave procedures) }
  102. requires_ecx_pic_helper,
  103. { generate pic helper which loads eip in ebx (for non leave procedures) }
  104. requires_ebx_pic_helper : boolean;
  105. interface_only: boolean; { interface-only macpas unit; flag does not need saving/restoring to ppu }
  106. mainfilepos : tfileposinfo;
  107. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  108. crc,
  109. interface_crc,
  110. indirect_crc : cardinal;
  111. flags : cardinal; { the PPU flags }
  112. islibrary : boolean; { if it is a library (win32 dll) }
  113. IsPackage : boolean;
  114. moduleid : longint;
  115. unitmap : punitmap; { mapping of all used units }
  116. unitmapsize : longint; { number of units in the map }
  117. derefmap : pderefmap; { mapping of all units needed for deref }
  118. derefmapcnt : longint; { number of units in the map }
  119. derefmapsize : longint; { number of units in the map }
  120. derefdataintflen : longint;
  121. derefdata : tdynamicarray;
  122. checkforwarddefs,
  123. deflist,
  124. symlist : TFPObjectList;
  125. ptrdefs : THashSet; { list of pointerdefs created in this module so we can reuse them (not saved/restored) }
  126. {$ifdef x86}
  127. x86ptrdefs : array [tx86pointertyp] of THashSet;
  128. {$endif x86}
  129. arraydefs : THashSet; { list of single-element-arraydefs created in this module so we can reuse them (not saved/restored) }
  130. ansistrdef : tobject; { an ansistring def redefined for the current module }
  131. wpoinfo : tunitwpoinfobase; { whole program optimization-related information that is generated during the current run for this unit }
  132. globalsymtable, { pointer to the global symtable of this unit }
  133. localsymtable : TSymtable;{ pointer to the local symtable of this unit }
  134. globalmacrosymtable, { pointer to the global macro symtable of this unit }
  135. localmacrosymtable : TSymtable;{ pointer to the local macro symtable of this unit }
  136. scanner : TObject; { scanner object used }
  137. procinfo : TObject; { current procedure being compiled }
  138. asmdata : TObject; { Assembler data }
  139. asmprefix : pshortstring; { prefix for the smartlink asmfiles }
  140. debuginfo : TObject;
  141. loaded_from : tmodule;
  142. _exports : tlinkedlist;
  143. dllscannerinputlist : TFPHashList;
  144. resourcefiles : TCmdStrList;
  145. linkunitofiles,
  146. linkunitstaticlibs,
  147. linkunitsharedlibs,
  148. linkotherofiles, { objects,libs loaded from the source }
  149. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  150. linkotherstaticlibs,
  151. linkotherframeworks : tlinkcontainer;
  152. mainname : pshortstring; { alternate name for "main" procedure }
  153. used_units : tlinkedlist;
  154. dependent_units : tlinkedlist;
  155. localunitsearchpath, { local searchpaths }
  156. localobjectsearchpath,
  157. localincludesearchpath,
  158. locallibrarysearchpath,
  159. localframeworksearchpath : TSearchPathList;
  160. moduleoptions: tmoduleoptions;
  161. deprecatedmsg: pshortstring;
  162. { contains a list of types that are extended by helper types; the key is
  163. the full name of the type and the data is a TFPObjectList of
  164. tobjectdef instances (the helper defs) }
  165. extendeddefs: TFPHashObjectList;
  166. { contains a list of the current topmost non-generic symbol for a
  167. typename of which at least one generic exists; the key is the
  168. non-generic typename and the data is a TFPObjectList of tgenericdummyentry
  169. instances whereby the last one is the current top most one }
  170. genericdummysyms: TFPHashObjectList;
  171. { this contains a list of units that needs to be waited for until the
  172. unit can be finished (code generated, etc.); this is needed to handle
  173. specializations in circular unit usages correctly }
  174. waitingforunit: tfpobjectlist;
  175. { this contains a list of all units that are waiting for this unit to be
  176. finished }
  177. waitingunits: tfpobjectlist;
  178. finishstate: pointer;
  179. globalstate: pointer;
  180. namespace: pshortstring; { for JVM target: corresponds to Java package name }
  181. { for targets that initialise typed constants via explicit assignments
  182. instead of by generating an initialised data section (holds typed
  183. constant assignments at the module level; does not have to be saved
  184. into the ppu file, because translated into code during compilation)
  185. -- actual type: tnode (but fmodule should not depend on node) }
  186. tcinitcode : tobject;
  187. {create creates a new module which name is stored in 's'. LoadedFrom
  188. points to the module calling it. It is nil for the first compiled
  189. module. This allow inheritence of all path lists. MUST pay attention
  190. to that when creating link.res!!!!(mazen)}
  191. constructor create(LoadedFrom:TModule;const amodulename: string; const afilename:TPathStr;_is_unit:boolean);
  192. destructor destroy;override;
  193. procedure reset;virtual;
  194. procedure adddependency(callermodule:tmodule);
  195. procedure flagdependent(callermodule:tmodule);
  196. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  197. procedure updatemaps;
  198. function derefidx_unit(id:longint):longint;
  199. function resolve_unit(id:longint):tmodule;
  200. procedure allunitsused;
  201. procedure end_of_parsing;virtual;
  202. procedure setmodulename(const s:string);
  203. procedure AddExternalImport(const libname,symname,symmangledname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  204. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  205. end;
  206. tused_unit = class(tlinkedlistitem)
  207. checksum,
  208. interface_checksum,
  209. indirect_checksum: cardinal;
  210. in_uses,
  211. in_interface : boolean;
  212. u : tmodule;
  213. unitsym : tunitsym;
  214. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  215. procedure check_hints;
  216. end;
  217. tdependent_unit = class(tlinkedlistitem)
  218. u : tmodule;
  219. constructor create(_u : tmodule);
  220. end;
  221. var
  222. main_module : tmodule; { Main module of the program }
  223. current_module : tmodule; { Current module which is compiled or loaded }
  224. compiled_module : tmodule; { Current module which is compiled }
  225. usedunits : tlinkedlist; { Used units for this program }
  226. loaded_units : tlinkedlist; { All loaded units }
  227. unloaded_units : tlinkedlist; { Units removed from loaded_units, to be freed }
  228. SmartLinkOFiles : TCmdStrList; { List of .o files which are generated,
  229. used to delete them after linking }
  230. procedure set_current_module(p:tmodule);
  231. function get_module(moduleindex : longint) : tmodule;
  232. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  233. procedure addloadedunit(hp:tmodule);
  234. function find_module_from_symtable(st:tsymtable):tmodule;
  235. implementation
  236. uses
  237. SysUtils,globals,
  238. verbose,systems,
  239. scanner,ppu,dbgbase,
  240. procinfo,symdef;
  241. {$ifdef MEMDEBUG}
  242. var
  243. memsymtable : TMemDebug;
  244. {$endif}
  245. {*****************************************************************************
  246. Global Functions
  247. *****************************************************************************}
  248. function find_module_from_symtable(st:tsymtable):tmodule;
  249. var
  250. hp : tmodule;
  251. begin
  252. result:=nil;
  253. hp:=tmodule(loaded_units.first);
  254. while assigned(hp) do
  255. begin
  256. if (hp.moduleid=st.moduleid) then
  257. begin
  258. result:=hp;
  259. exit;
  260. end;
  261. hp:=tmodule(hp.next);
  262. end;
  263. end;
  264. procedure set_current_module(p:tmodule);
  265. begin
  266. { save the state of the scanner }
  267. if assigned(current_scanner) then
  268. current_scanner.tempcloseinputfile;
  269. { set new module }
  270. current_module:=p;
  271. { restore previous module settings }
  272. Fillchar(current_filepos,0,sizeof(current_filepos));
  273. if assigned(current_module) then
  274. begin
  275. current_asmdata:=tasmdata(current_module.asmdata);
  276. current_debuginfo:=tdebuginfo(current_module.debuginfo);
  277. { restore scanner and file positions }
  278. current_scanner:=tscannerfile(current_module.scanner);
  279. if assigned(current_scanner) then
  280. begin
  281. current_scanner.tempopeninputfile;
  282. current_scanner.gettokenpos;
  283. parser_current_file:=current_scanner.inputfile.name;
  284. end
  285. else
  286. begin
  287. current_filepos.moduleindex:=current_module.unit_index;
  288. parser_current_file:='';
  289. end;
  290. end
  291. else
  292. begin
  293. current_asmdata:=nil;
  294. current_scanner:=nil;
  295. current_debuginfo:=nil;
  296. end;
  297. end;
  298. function get_module(moduleindex : longint) : tmodule;
  299. var
  300. hp : tmodule;
  301. begin
  302. result:=nil;
  303. if moduleindex=0 then
  304. exit;
  305. result:=current_module;
  306. if not(assigned(loaded_units)) then
  307. exit;
  308. hp:=tmodule(loaded_units.first);
  309. while assigned(hp) and (hp.unit_index<>moduleindex) do
  310. hp:=tmodule(hp.next);
  311. result:=hp;
  312. end;
  313. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  314. var
  315. hp : tmodule;
  316. begin
  317. hp:=get_module(moduleindex);
  318. if assigned(hp) then
  319. get_source_file:=hp.sourcefiles.get_file(fileindex)
  320. else
  321. get_source_file:=nil;
  322. end;
  323. procedure addloadedunit(hp:tmodule);
  324. begin
  325. hp.moduleid:=loaded_units.count;
  326. loaded_units.concat(hp);
  327. end;
  328. {****************************************************************************
  329. TLinkContainerItem
  330. ****************************************************************************}
  331. constructor TLinkContainerItem.Create(const s:TPathStr;m:cardinal);
  332. begin
  333. inherited Create;
  334. data:=s;
  335. needlink:=m;
  336. end;
  337. {****************************************************************************
  338. TLinkContainer
  339. ****************************************************************************}
  340. procedure TLinkContainer.add(const s : TPathStr;m:cardinal);
  341. begin
  342. inherited concat(TLinkContainerItem.Create(s,m));
  343. end;
  344. function TLinkContainer.get(var m:cardinal) : TPathStr;
  345. var
  346. p : tlinkcontaineritem;
  347. begin
  348. p:=tlinkcontaineritem(inherited getfirst);
  349. if p=nil then
  350. begin
  351. get:='';
  352. m:=0;
  353. end
  354. else
  355. begin
  356. get:=p.data;
  357. m:=p.needlink;
  358. p.free;
  359. end;
  360. end;
  361. function TLinkContainer.getusemask(mask:cardinal) : TPathStr;
  362. var
  363. p : tlinkcontaineritem;
  364. found : boolean;
  365. begin
  366. found:=false;
  367. repeat
  368. p:=tlinkcontaineritem(inherited getfirst);
  369. if p=nil then
  370. begin
  371. getusemask:='';
  372. exit;
  373. end;
  374. getusemask:=p.data;
  375. found:=(p.needlink and mask)<>0;
  376. p.free;
  377. until found;
  378. end;
  379. function TLinkContainer.find(const s:TPathStr):boolean;
  380. var
  381. newnode : tlinkcontaineritem;
  382. begin
  383. find:=false;
  384. newnode:=tlinkcontaineritem(First);
  385. while assigned(newnode) do
  386. begin
  387. if newnode.data=s then
  388. begin
  389. find:=true;
  390. exit;
  391. end;
  392. newnode:=tlinkcontaineritem(newnode.next);
  393. end;
  394. end;
  395. {****************************************************************************
  396. TUSED_UNIT
  397. ****************************************************************************}
  398. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  399. begin
  400. u:=_u;
  401. in_interface:=intface;
  402. in_uses:=inuses;
  403. unitsym:=usym;
  404. if _u.state=ms_compiled then
  405. begin
  406. checksum:=u.crc;
  407. interface_checksum:=u.interface_crc;
  408. indirect_checksum:=u.indirect_crc;
  409. end
  410. else
  411. begin
  412. checksum:=0;
  413. interface_checksum:=0;
  414. indirect_checksum:=0;
  415. end;
  416. end;
  417. procedure tused_unit.check_hints;
  418. var
  419. uname: pshortstring;
  420. begin
  421. uname:=u.realmodulename;
  422. if mo_hint_deprecated in u.moduleoptions then
  423. if (mo_has_deprecated_msg in u.moduleoptions) and (u.deprecatedmsg <> nil) then
  424. MessagePos2(unitsym.fileinfo,sym_w_deprecated_unit_with_msg,uname^,u.deprecatedmsg^)
  425. else
  426. MessagePos1(unitsym.fileinfo,sym_w_deprecated_unit,uname^);
  427. if mo_hint_experimental in u.moduleoptions then
  428. MessagePos1(unitsym.fileinfo,sym_w_experimental_unit,uname^);
  429. if mo_hint_platform in u.moduleoptions then
  430. MessagePos1(unitsym.fileinfo,sym_w_non_portable_unit,uname^);
  431. if mo_hint_library in u.moduleoptions then
  432. MessagePos1(unitsym.fileinfo,sym_w_library_unit,uname^);
  433. if mo_hint_unimplemented in u.moduleoptions then
  434. MessagePos1(unitsym.fileinfo,sym_w_non_implemented_unit,uname^);
  435. end;
  436. {****************************************************************************
  437. TDENPENDENT_UNIT
  438. ****************************************************************************}
  439. constructor tdependent_unit.create(_u : tmodule);
  440. begin
  441. u:=_u;
  442. end;
  443. {****************************************************************************
  444. TMODULE
  445. ****************************************************************************}
  446. constructor tmodule.create(LoadedFrom:TModule;const amodulename: string; const afilename:TPathStr;_is_unit:boolean);
  447. var
  448. n:string;
  449. fn:TPathStr;
  450. {$ifdef x86}
  451. ptrtyp:tx86pointertyp;
  452. {$endif x86}
  453. begin
  454. if amodulename='' then
  455. n:=ChangeFileExt(ExtractFileName(afilename),'')
  456. else
  457. n:=amodulename;
  458. if afilename='' then
  459. fn:=amodulename
  460. else
  461. fn:=afilename;
  462. { Programs have the name 'Program' to don't conflict with dup id's }
  463. if _is_unit then
  464. inherited create(amodulename)
  465. else
  466. inherited create('Program');
  467. mainsource:=fn;
  468. { Dos has the famous 8.3 limit :( }
  469. {$ifdef shortasmprefix}
  470. asmprefix:=stringdup(FixFileName('as'));
  471. {$else}
  472. asmprefix:=stringdup(FixFileName(n));
  473. {$endif}
  474. setfilename(fn,true);
  475. localunitsearchpath:=TSearchPathList.Create;
  476. localobjectsearchpath:=TSearchPathList.Create;
  477. localincludesearchpath:=TSearchPathList.Create;
  478. locallibrarysearchpath:=TSearchPathList.Create;
  479. localframeworksearchpath:=TSearchPathList.Create;
  480. used_units:=TLinkedList.Create;
  481. dependent_units:=TLinkedList.Create;
  482. resourcefiles:=TCmdStrList.Create;
  483. linkunitofiles:=TLinkContainer.Create;
  484. linkunitstaticlibs:=TLinkContainer.Create;
  485. linkunitsharedlibs:=TLinkContainer.Create;
  486. linkotherofiles:=TLinkContainer.Create;
  487. linkotherstaticlibs:=TLinkContainer.Create;
  488. linkothersharedlibs:=TLinkContainer.Create;
  489. linkotherframeworks:=TLinkContainer.Create;
  490. mainname:=nil;
  491. FImportLibraryList:=TFPHashObjectList.Create(true);
  492. crc:=0;
  493. interface_crc:=0;
  494. indirect_crc:=0;
  495. flags:=0;
  496. scanner:=nil;
  497. unitmap:=nil;
  498. unitmapsize:=0;
  499. derefmap:=nil;
  500. derefmapsize:=0;
  501. derefmapcnt:=0;
  502. derefdata:=TDynamicArray.Create(1024);
  503. derefdataintflen:=0;
  504. deflist:=TFPObjectList.Create(false);
  505. symlist:=TFPObjectList.Create(false);
  506. {$ifdef x86}
  507. for ptrtyp in tx86pointertyp do
  508. x86ptrdefs[ptrtyp]:=THashSet.Create(64,true,false);
  509. ptrdefs:=x86ptrdefs[tcpupointerdefclass(cpointerdef).default_x86_data_pointer_type];
  510. {$else x86}
  511. ptrdefs:=THashSet.Create(64,true,false);
  512. {$endif x86}
  513. arraydefs:=THashSet.Create(64,true,false);
  514. ansistrdef:=nil;
  515. wpoinfo:=nil;
  516. checkforwarddefs:=TFPObjectList.Create(false);
  517. extendeddefs:=TFPHashObjectList.Create(true);
  518. genericdummysyms:=tfphashobjectlist.create(true);
  519. waitingforunit:=tfpobjectlist.create(false);
  520. waitingunits:=tfpobjectlist.create(false);
  521. globalsymtable:=nil;
  522. localsymtable:=nil;
  523. globalmacrosymtable:=nil;
  524. localmacrosymtable:=nil;
  525. loaded_from:=LoadedFrom;
  526. do_reload:=false;
  527. do_compile:=false;
  528. sources_avail:=true;
  529. mainfilepos.line:=0;
  530. mainfilepos.column:=0;
  531. mainfilepos.fileindex:=0;
  532. recompile_reason:=rr_unknown;
  533. in_interface:=true;
  534. in_global:=true;
  535. is_unit:=_is_unit;
  536. islibrary:=false;
  537. ispackage:=false;
  538. is_dbginfo_written:=false;
  539. mode_switch_allowed:= true;
  540. moduleoptions:=[];
  541. deprecatedmsg:=nil;
  542. namespace:=nil;
  543. tcinitcode:=nil;
  544. _exports:=TLinkedList.Create;
  545. dllscannerinputlist:=TFPHashList.Create;
  546. asmdata:=casmdata.create(modulename);
  547. InitDebugInfo(self,false);
  548. end;
  549. destructor tmodule.Destroy;
  550. var
  551. i : longint;
  552. current_debuginfo_reset : boolean;
  553. {$ifdef x86}
  554. ptrtyp : tx86pointertyp;
  555. {$endif x86}
  556. begin
  557. if assigned(unitmap) then
  558. freemem(unitmap);
  559. if assigned(derefmap) then
  560. begin
  561. for i:=0 to derefmapcnt-1 do
  562. stringdispose(derefmap[i].modulename);
  563. freemem(derefmap);
  564. end;
  565. if assigned(_exports) then
  566. _exports.free;
  567. if assigned(dllscannerinputlist) then
  568. dllscannerinputlist.free;
  569. if assigned(scanner) then
  570. begin
  571. { also update current_scanner if it was pointing
  572. to this module }
  573. if current_scanner=tscannerfile(scanner) then
  574. current_scanner:=nil;
  575. tscannerfile(scanner).free;
  576. end;
  577. if assigned(asmdata) then
  578. begin
  579. if current_asmdata=asmdata then
  580. current_asmdata:=nil;
  581. asmdata.free;
  582. end;
  583. if assigned(procinfo) then
  584. begin
  585. if current_procinfo=tprocinfo(procinfo) then
  586. begin
  587. current_procinfo:=nil;
  588. current_structdef:=nil;
  589. current_genericdef:=nil;
  590. current_specializedef:=nil;
  591. end;
  592. { release procinfo tree }
  593. tprocinfo(procinfo).destroy_tree;
  594. end;
  595. DoneDebugInfo(self,current_debuginfo_reset);
  596. used_units.free;
  597. dependent_units.free;
  598. resourcefiles.Free;
  599. linkunitofiles.Free;
  600. linkunitstaticlibs.Free;
  601. linkunitsharedlibs.Free;
  602. linkotherofiles.Free;
  603. linkotherstaticlibs.Free;
  604. linkothersharedlibs.Free;
  605. linkotherframeworks.Free;
  606. stringdispose(mainname);
  607. FImportLibraryList.Free;
  608. extendeddefs.Free;
  609. genericdummysyms.free;
  610. waitingforunit.free;
  611. waitingunits.free;
  612. stringdispose(asmprefix);
  613. stringdispose(deprecatedmsg);
  614. stringdispose(namespace);
  615. tcinitcode.free;
  616. localunitsearchpath.Free;
  617. localobjectsearchpath.free;
  618. localincludesearchpath.free;
  619. locallibrarysearchpath.free;
  620. localframeworksearchpath.free;
  621. {$ifdef MEMDEBUG}
  622. memsymtable.start;
  623. {$endif}
  624. derefdata.free;
  625. deflist.free;
  626. symlist.free;
  627. {$ifdef x86}
  628. for ptrtyp in tx86pointertyp do
  629. x86ptrdefs[ptrtyp].free;
  630. ptrdefs:=nil;
  631. {$else x86}
  632. ptrdefs.free;
  633. {$endif x86}
  634. arraydefs.free;
  635. ansistrdef:=nil;
  636. wpoinfo.free;
  637. checkforwarddefs.free;
  638. globalsymtable.free;
  639. localsymtable.free;
  640. globalmacrosymtable.free;
  641. localmacrosymtable.free;
  642. {$ifdef MEMDEBUG}
  643. memsymtable.stop;
  644. {$endif}
  645. inherited Destroy;
  646. end;
  647. procedure tmodule.reset;
  648. var
  649. i : longint;
  650. current_debuginfo_reset : boolean;
  651. {$ifdef x86}
  652. ptrtyp : tx86pointertyp;
  653. {$endif x86}
  654. begin
  655. if assigned(scanner) then
  656. begin
  657. { also update current_scanner if it was pointing
  658. to this module }
  659. if current_scanner=tscannerfile(scanner) then
  660. current_scanner:=nil;
  661. tscannerfile(scanner).free;
  662. scanner:=nil;
  663. end;
  664. if assigned(procinfo) then
  665. begin
  666. if current_procinfo=tprocinfo(procinfo) then
  667. begin
  668. current_procinfo:=nil;
  669. current_structdef:=nil;
  670. current_genericdef:=nil;
  671. current_specializedef:=nil;
  672. end;
  673. { release procinfo tree }
  674. tprocinfo(procinfo).destroy_tree;
  675. end;
  676. if assigned(asmdata) then
  677. begin
  678. if current_asmdata=asmdata then
  679. current_asmdata:=nil;
  680. asmdata.free;
  681. asmdata:=nil;
  682. end;
  683. DoneDebugInfo(self,current_debuginfo_reset);
  684. globalsymtable.free;
  685. globalsymtable:=nil;
  686. localsymtable.free;
  687. localsymtable:=nil;
  688. globalmacrosymtable.free;
  689. globalmacrosymtable:=nil;
  690. localmacrosymtable.free;
  691. localmacrosymtable:=nil;
  692. deflist.free;
  693. deflist:=TFPObjectList.Create(false);
  694. symlist.free;
  695. symlist:=TFPObjectList.Create(false);
  696. {$ifdef x86}
  697. for ptrtyp in tx86pointertyp do
  698. begin
  699. x86ptrdefs[ptrtyp].free;
  700. x86ptrdefs[ptrtyp]:=THashSet.Create(64,true,false);
  701. end;
  702. ptrdefs:=x86ptrdefs[tcpupointerdefclass(cpointerdef).default_x86_data_pointer_type];
  703. {$else x86}
  704. ptrdefs.free;
  705. ptrdefs:=THashSet.Create(64,true,false);
  706. {$endif x86}
  707. arraydefs.free;
  708. arraydefs:=THashSet.Create(64,true,false);
  709. wpoinfo.free;
  710. wpoinfo:=nil;
  711. checkforwarddefs.free;
  712. checkforwarddefs:=TFPObjectList.Create(false);
  713. derefdata.free;
  714. derefdata:=TDynamicArray.Create(1024);
  715. if assigned(unitmap) then
  716. begin
  717. freemem(unitmap);
  718. unitmap:=nil;
  719. end;
  720. if assigned(derefmap) then
  721. begin
  722. for i:=0 to derefmapcnt-1 do
  723. stringdispose(derefmap[i].modulename);
  724. freemem(derefmap);
  725. derefmap:=nil;
  726. end;
  727. unitmapsize:=0;
  728. derefmapsize:=0;
  729. derefmapcnt:=0;
  730. derefdataintflen:=0;
  731. sourcefiles.free;
  732. sourcefiles:=tinputfilemanager.create;
  733. asmdata:=casmdata.create(modulename);
  734. InitDebugInfo(self,current_debuginfo_reset);
  735. _exports.free;
  736. _exports:=tlinkedlist.create;
  737. dllscannerinputlist.free;
  738. dllscannerinputlist:=TFPHashList.create;
  739. used_units.free;
  740. used_units:=TLinkedList.Create;
  741. dependent_units.free;
  742. dependent_units:=TLinkedList.Create;
  743. resourcefiles.Free;
  744. resourcefiles:=TCmdStrList.Create;
  745. linkunitofiles.Free;
  746. linkunitofiles:=TLinkContainer.Create;
  747. linkunitstaticlibs.Free;
  748. linkunitstaticlibs:=TLinkContainer.Create;
  749. linkunitsharedlibs.Free;
  750. linkunitsharedlibs:=TLinkContainer.Create;
  751. linkotherofiles.Free;
  752. linkotherofiles:=TLinkContainer.Create;
  753. linkotherstaticlibs.Free;
  754. linkotherstaticlibs:=TLinkContainer.Create;
  755. linkothersharedlibs.Free;
  756. linkothersharedlibs:=TLinkContainer.Create;
  757. linkotherframeworks.Free;
  758. linkotherframeworks:=TLinkContainer.Create;
  759. stringdispose(mainname);
  760. FImportLibraryList.Free;
  761. FImportLibraryList:=TFPHashObjectList.Create;
  762. do_compile:=false;
  763. do_reload:=false;
  764. interface_compiled:=false;
  765. in_interface:=true;
  766. in_global:=true;
  767. mode_switch_allowed:=true;
  768. stringdispose(deprecatedmsg);
  769. stringdispose(namespace);
  770. tcinitcode.free;
  771. tcinitcode:=nil;
  772. moduleoptions:=[];
  773. is_dbginfo_written:=false;
  774. crc:=0;
  775. interface_crc:=0;
  776. indirect_crc:=0;
  777. flags:=0;
  778. mainfilepos.line:=0;
  779. mainfilepos.column:=0;
  780. mainfilepos.fileindex:=0;
  781. recompile_reason:=rr_unknown;
  782. {
  783. The following fields should not
  784. be reset:
  785. mainsource
  786. state
  787. loaded_from
  788. sources_avail
  789. }
  790. end;
  791. procedure tmodule.adddependency(callermodule:tmodule);
  792. begin
  793. { This is not needed for programs }
  794. if not callermodule.is_unit then
  795. exit;
  796. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  797. dependent_units.concat(tdependent_unit.create(callermodule));
  798. end;
  799. procedure tmodule.flagdependent(callermodule:tmodule);
  800. var
  801. pm : tdependent_unit;
  802. begin
  803. { flag all units that depend on this unit for reloading }
  804. pm:=tdependent_unit(current_module.dependent_units.first);
  805. while assigned(pm) do
  806. begin
  807. { We do not have to reload the unit that wants to load
  808. this unit, unless this unit is already compiled during
  809. the loading }
  810. if (pm.u=callermodule) and
  811. (pm.u.state<>ms_compiled) then
  812. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  813. else
  814. if pm.u.state=ms_second_compile then
  815. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  816. else
  817. begin
  818. pm.u.do_reload:=true;
  819. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  820. end;
  821. pm:=tdependent_unit(pm.next);
  822. end;
  823. end;
  824. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  825. var
  826. pu : tused_unit;
  827. begin
  828. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  829. used_units.concat(pu);
  830. addusedunit:=pu;
  831. end;
  832. procedure tmodule.updatemaps;
  833. var
  834. oldmapsize : longint;
  835. hp : tmodule;
  836. i : longint;
  837. begin
  838. { Extend unitmap }
  839. oldmapsize:=unitmapsize;
  840. unitmapsize:=loaded_units.count;
  841. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  842. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  843. { Extend Derefmap }
  844. oldmapsize:=derefmapsize;
  845. derefmapsize:=loaded_units.count;
  846. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  847. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  848. { Add all units to unitmap }
  849. hp:=tmodule(loaded_units.first);
  850. i:=0;
  851. while assigned(hp) do
  852. begin
  853. if hp.moduleid>=unitmapsize then
  854. internalerror(200501151);
  855. { Verify old entries }
  856. if (i<oldmapsize) then
  857. begin
  858. if (hp.moduleid<>i) or
  859. (unitmap[hp.moduleid].u<>hp) then
  860. internalerror(200501156);
  861. end
  862. else
  863. begin
  864. unitmap[hp.moduleid].u:=hp;
  865. unitmap[hp.moduleid].derefidx:=-1;
  866. end;
  867. inc(i);
  868. hp:=tmodule(hp.next);
  869. end;
  870. end;
  871. function tmodule.derefidx_unit(id:longint):longint;
  872. begin
  873. if id>=unitmapsize then
  874. internalerror(2005011511);
  875. if unitmap[id].derefidx=-1 then
  876. begin
  877. unitmap[id].derefidx:=derefmapcnt;
  878. inc(derefmapcnt);
  879. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  880. end;
  881. if unitmap[id].derefidx>=derefmapsize then
  882. internalerror(2005011514);
  883. result:=unitmap[id].derefidx;
  884. end;
  885. function tmodule.resolve_unit(id:longint):tmodule;
  886. var
  887. hp : tmodule;
  888. begin
  889. if id>=derefmapsize then
  890. internalerror(200306231);
  891. result:=derefmap[id].u;
  892. if not assigned(result) then
  893. begin
  894. if not assigned(derefmap[id].modulename) or
  895. (derefmap[id].modulename^='') then
  896. internalerror(200501159);
  897. hp:=tmodule(loaded_units.first);
  898. while assigned(hp) do
  899. begin
  900. { only check for units. The main program is also
  901. as a unit in the loaded_units list. We simply need
  902. to ignore this entry (PFV) }
  903. if hp.is_unit and
  904. (hp.modulename^=derefmap[id].modulename^) then
  905. break;
  906. hp:=tmodule(hp.next);
  907. end;
  908. if not assigned(hp) then
  909. internalerror(2005011510);
  910. derefmap[id].u:=hp;
  911. result:=hp;
  912. end;
  913. end;
  914. procedure tmodule.allunitsused;
  915. var
  916. pu : tused_unit;
  917. begin
  918. pu:=tused_unit(used_units.first);
  919. while assigned(pu) do
  920. begin
  921. if assigned(pu.u.globalsymtable) then
  922. begin
  923. if unitmap[pu.u.moduleid].u<>pu.u then
  924. internalerror(200501157);
  925. { Give a note when the unit is not referenced, skip
  926. this is for units with an initialization/finalization }
  927. if (unitmap[pu.u.moduleid].refs=0) and
  928. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  929. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  930. end;
  931. pu:=tused_unit(pu.next);
  932. end;
  933. end;
  934. procedure tmodule.end_of_parsing;
  935. begin
  936. { free asmdata }
  937. if assigned(asmdata) then
  938. begin
  939. asmdata.free;
  940. asmdata:=nil;
  941. end;
  942. { free scanner }
  943. if assigned(scanner) then
  944. begin
  945. if current_scanner=tscannerfile(scanner) then
  946. current_scanner:=nil;
  947. tscannerfile(scanner).free;
  948. scanner:=nil;
  949. end;
  950. { free symtable stack }
  951. if assigned(symtablestack) then
  952. begin
  953. symtablestack.free;
  954. symtablestack:=nil;
  955. end;
  956. if assigned(macrosymtablestack) then
  957. begin
  958. macrosymtablestack.free;
  959. macrosymtablestack:=nil;
  960. end;
  961. end;
  962. procedure tmodule.setmodulename(const s:string);
  963. begin
  964. stringdispose(modulename);
  965. stringdispose(realmodulename);
  966. modulename:=stringdup(upper(s));
  967. realmodulename:=stringdup(s);
  968. { also update asmlibrary names }
  969. current_asmdata.name:=modulename;
  970. end;
  971. procedure TModule.AddExternalImport(const libname,symname,symmangledname:string;
  972. OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  973. var
  974. ImportLibrary,OtherIL : TImportLibrary;
  975. ImportSymbol : TImportSymbol;
  976. i : longint;
  977. begin
  978. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  979. if not assigned(ImportLibrary) then
  980. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  981. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  982. if not assigned(ImportSymbol) then
  983. begin
  984. { Check that the same name does not exist in another library }
  985. { If it does and the same mangled name is used, issue a warning }
  986. if ImportLibraryList.Count>1 then
  987. for i:=0 To ImportLibraryList.Count-1 do
  988. begin
  989. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  990. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  991. if assigned(ImportSymbol) then
  992. begin
  993. if ImportSymbol.MangledName=symmangledname then
  994. begin
  995. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  996. break;
  997. end;
  998. end;
  999. end;
  1000. if not ImportByOrdinalOnly then
  1001. { negative ordinal number indicates import by name with ordinal number as hint }
  1002. OrdNr:=-OrdNr;
  1003. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  1004. symname,symmangledname,OrdNr,isvar);
  1005. end;
  1006. end;
  1007. initialization
  1008. {$ifdef MEMDEBUG}
  1009. memsymtable:=TMemDebug.create('Symtables');
  1010. memsymtable.stop;
  1011. {$endif MEMDEBUG}
  1012. finalization
  1013. {$ifdef MEMDEBUG}
  1014. memsymtable.free;
  1015. {$endif MEMDEBUG}
  1016. end.