fmodule.pas 42 KB

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