fmodule.pas 42 KB

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