fmodule.pas 37 KB

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