fmodule.pas 49 KB

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