fmodule.pas 49 KB

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