fmodule.pas 38 KB

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