fmodule.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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,
  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 : tunitmaparray; { mapping of all used units }
  122. unitmapsize : longint; { number of units in the map }
  123. derefmap : tderefmaparray; { 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. unitmap:=nil;
  610. if assigned(derefmap) then
  611. begin
  612. for i:=0 to derefmapcnt-1 do
  613. stringdispose(derefmap[i].modulename);
  614. end;
  615. derefmap:=nil;
  616. if assigned(_exports) then
  617. _exports.free;
  618. if assigned(dllscannerinputlist) then
  619. dllscannerinputlist.free;
  620. if assigned(scanner) then
  621. begin
  622. { also update current_scanner if it was pointing
  623. to this module }
  624. if current_scanner=tscannerfile(scanner) then
  625. set_current_scanner(nil);
  626. freeandnil(scanner);
  627. end;
  628. if assigned(asmdata) then
  629. begin
  630. if current_asmdata=asmdata then
  631. current_asmdata:=nil;
  632. asmdata.free;
  633. end;
  634. if assigned(procinfo) then
  635. begin
  636. if current_procinfo=tprocinfo(procinfo) then
  637. begin
  638. current_procinfo:=nil;
  639. current_structdef:=nil;
  640. current_genericdef:=nil;
  641. current_specializedef:=nil;
  642. end;
  643. { release procinfo tree }
  644. tprocinfo(procinfo).destroy_tree;
  645. procinfo := nil;
  646. end;
  647. DoneDebugInfo(self,current_debuginfo_reset);
  648. used_units.free;
  649. dependent_units.free;
  650. resourcefiles.Free;
  651. linkorderedsymbols.Free;
  652. linkunitofiles.Free;
  653. linkunitstaticlibs.Free;
  654. linkunitsharedlibs.Free;
  655. linkotherofiles.Free;
  656. linkotherstaticlibs.Free;
  657. linkothersharedlibs.Free;
  658. linkotherframeworks.Free;
  659. stringdispose(mainname);
  660. externasmsyms.Free;
  661. publicasmsyms.Free;
  662. unitimportsyms.Free;
  663. FImportLibraryList.Free;
  664. extendeddefs.Free;
  665. genericdummysyms.free;
  666. pendingspecializations.free;
  667. waitingforunit.free;
  668. waitingunits.free;
  669. used_rtti_attrs.free;
  670. stringdispose(asmprefix);
  671. stringdispose(deprecatedmsg);
  672. stringdispose(namespace);
  673. tcinitcode.free;
  674. localunitsearchpath.Free;
  675. localobjectsearchpath.free;
  676. localincludesearchpath.free;
  677. locallibrarysearchpath.free;
  678. localframeworksearchpath.free;
  679. {$ifdef MEMDEBUG}
  680. memsymtable.start;
  681. {$endif}
  682. derefdata.free;
  683. if assigned(deflist) then
  684. begin
  685. for i:=0 to deflist.Count-1 do
  686. if assigned(deflist[i]) and
  687. (tdef(deflist[i]).registered_in_module=self) then
  688. tdef(deflist[i]).registered_in_module:=nil;
  689. deflist.free;
  690. end;
  691. symlist.free;
  692. ptrdefs.free;
  693. arraydefs.free;
  694. procaddrdefs.free;
  695. {$ifdef llvm}
  696. llvmdefs.free;
  697. llvmusedsyms.free;
  698. llvmcompilerusedsyms.free;
  699. llvminitprocs.free;
  700. llvmfiniprocs.free;
  701. llvmmetadatastrings.free;
  702. {$endif llvm}
  703. ansistrdef:=nil;
  704. wpoinfo.free;
  705. checkforwarddefs.free;
  706. forwardgenericdefs.free;
  707. globalsymtable.free;
  708. localsymtable.free;
  709. globalmacrosymtable.free;
  710. localmacrosymtable.free;
  711. {$ifdef MEMDEBUG}
  712. memsymtable.stop;
  713. {$endif}
  714. inherited Destroy;
  715. end;
  716. procedure tmodule.reset(for_recompile: boolean);
  717. var
  718. i : longint;
  719. current_debuginfo_reset : boolean;
  720. m : tmodule;
  721. begin
  722. is_reset:=true;
  723. LoadCount:=0;
  724. if assigned(scanner) then
  725. begin
  726. { also update current_scanner if it was pointing
  727. to this module }
  728. if current_scanner=tscannerfile(scanner) then
  729. set_current_scanner(nil);
  730. freeandnil(scanner);
  731. end;
  732. if assigned(procinfo) then
  733. begin
  734. if current_procinfo=tprocinfo(procinfo) then
  735. begin
  736. current_procinfo:=nil;
  737. current_structdef:=nil;
  738. current_genericdef:=nil;
  739. current_specializedef:=nil;
  740. end;
  741. { release procinfo tree }
  742. tprocinfo(procinfo).destroy_tree;
  743. end;
  744. if assigned(asmdata) then
  745. begin
  746. if current_asmdata=asmdata then
  747. current_asmdata:=nil;
  748. asmdata.free;
  749. asmdata:=nil;
  750. end;
  751. DoneDebugInfo(self,current_debuginfo_reset);
  752. globalsymtable.free;
  753. globalsymtable:=nil;
  754. localsymtable.free;
  755. localsymtable:=nil;
  756. globalmacrosymtable.free;
  757. globalmacrosymtable:=nil;
  758. localmacrosymtable.free;
  759. localmacrosymtable:=nil;
  760. deflist.free;
  761. deflist:=TFPObjectList.Create(false);
  762. symlist.free;
  763. symlist:=TFPObjectList.Create(false);
  764. ptrdefs.free;
  765. ptrdefs:=THashSet.Create(64,true,false);
  766. arraydefs.free;
  767. arraydefs:=THashSet.Create(64,true,false);
  768. procaddrdefs.free;
  769. procaddrdefs:=THashSet.Create(64,true,false);
  770. {$ifdef llvm}
  771. llvmdefs.free;
  772. llvmdefs:=THashSet.Create(64,true,false);
  773. llvmusedsyms.free;
  774. llvmusedsyms:=TFPObjectList.Create(true);
  775. llvmcompilerusedsyms.free;
  776. llvmcompilerusedsyms:=TFPObjectList.Create(true);
  777. llvminitprocs.free;
  778. llvminitprocs:=TFPList.Create;
  779. llvmfiniprocs.free;
  780. llvmfiniprocs:=TFPList.Create;
  781. llvmmetadatastrings.free;
  782. llvmmetadatastrings:=TFPHashList.Create;
  783. {$endif llvm}
  784. ansistrdef:=nil;
  785. wpoinfo.free;
  786. wpoinfo:=nil;
  787. checkforwarddefs.free;
  788. checkforwarddefs:=TFPObjectList.Create(false);
  789. forwardgenericdefs.free;
  790. forwardgenericdefs:=TFPHashObjectList.Create(true);
  791. publicasmsyms.free;
  792. publicasmsyms:=TFPHashObjectList.Create(true);
  793. externasmsyms.free;
  794. externasmsyms:=TFPHashObjectList.Create(true);
  795. unitimportsyms.free;
  796. unitimportsyms:=TFPObjectList.Create(false);
  797. derefdata.free;
  798. derefdata:=TDynamicArray.Create(1024);
  799. unitmap:=nil;
  800. if assigned(derefmap) then
  801. begin
  802. for i:=0 to derefmapcnt-1 do
  803. stringdispose(derefmap[i].modulename);
  804. end;
  805. derefmap:=nil;
  806. unitmapsize:=0;
  807. derefmapsize:=0;
  808. derefmapcnt:=0;
  809. derefdataintflen:=0;
  810. sourcefiles.free;
  811. sourcefiles:=tinputfilemanager.create;
  812. asmdata:=casmdata.create(modulename);
  813. InitDebugInfo(self,current_debuginfo_reset);
  814. _exports.free;
  815. _exports:=tlinkedlist.create;
  816. dllscannerinputlist.free;
  817. dllscannerinputlist:=TFPHashList.create;
  818. { During reload, the list of used units cannot change.
  819. It can only change while recompiling.
  820. Because the used_units is used in loops in the load cycle(s) which
  821. can recurse into the same unit due to circular dependencies,
  822. we do not destroy the list, we only update the contents.
  823. As a result so the loop variable does not get reset during the loop.
  824. For recompile, we recreate the list }
  825. if for_recompile then
  826. begin
  827. used_units.free;
  828. used_units:=TLinkedList.Create;
  829. end;
  830. dependent_units.free;
  831. dependent_units:=TLinkedList.Create;
  832. resourcefiles.Free;
  833. resourcefiles:=TCmdStrList.Create;
  834. linkorderedsymbols.Free;
  835. linkorderedsymbols:=TCmdStrList.Create;
  836. pendingspecializations.free;
  837. pendingspecializations:=tfphashobjectlist.create(false);
  838. genericdummysyms.Free;
  839. genericdummysyms := tfphashobjectlist.create(true);
  840. extendeddefs.Free;
  841. extendeddefs:=TFPHashObjectList.Create(true);
  842. if assigned(waitingforunit) and
  843. (waitingforunit.count<>0) then
  844. begin
  845. Write(Self.modulename^, ' is reset while still waiting for units: ');
  846. for I:=0 to waitingforunit.Count-1 do
  847. begin
  848. M:=tmodule(waitingforunit.Items[i]);
  849. write(m.modulename^,' (state:',M.state,') ');
  850. end;
  851. Writeln;
  852. internalerror(2016070501);
  853. end;
  854. waitingforunit.free;
  855. waitingforunit:=tfpobjectlist.create(false);
  856. linkunitofiles.Free;
  857. linkunitofiles:=TLinkContainer.Create;
  858. linkunitstaticlibs.Free;
  859. linkunitstaticlibs:=TLinkContainer.Create;
  860. linkunitsharedlibs.Free;
  861. linkunitsharedlibs:=TLinkContainer.Create;
  862. linkotherofiles.Free;
  863. linkotherofiles:=TLinkContainer.Create;
  864. linkotherstaticlibs.Free;
  865. linkotherstaticlibs:=TLinkContainer.Create;
  866. linkothersharedlibs.Free;
  867. linkothersharedlibs:=TLinkContainer.Create;
  868. linkotherframeworks.Free;
  869. linkotherframeworks:=TLinkContainer.Create;
  870. stringdispose(mainname);
  871. FImportLibraryList.Free;
  872. FImportLibraryList:=TFPHashObjectList.Create;
  873. do_reload:=false;
  874. interface_compiled:=false;
  875. in_interface:=true;
  876. in_global:=true;
  877. mode_switch_allowed:=true;
  878. stringdispose(deprecatedmsg);
  879. stringdispose(namespace);
  880. tcinitcode.free;
  881. tcinitcode:=nil;
  882. localunitsearchpath.Free;
  883. localunitsearchpath:=TSearchPathList.Create;
  884. localobjectsearchpath.free;
  885. localobjectsearchpath:=TSearchPathList.Create;
  886. localincludesearchpath.free;
  887. localincludesearchpath:=TSearchPathList.Create;
  888. locallibrarysearchpath.free;
  889. locallibrarysearchpath:=TSearchPathList.Create;
  890. localframeworksearchpath.free;
  891. localframeworksearchpath:=TSearchPathList.Create;
  892. moduleoptions:=[];
  893. is_dbginfo_written:=false;
  894. crc:=0;
  895. interface_crc:=0;
  896. indirect_crc:=0;
  897. headerflags:=0;
  898. longversion:=0;
  899. moduleflags:=[];
  900. mainfilepos.line:=0;
  901. mainfilepos.column:=0;
  902. mainfilepos.fileindex:=0;
  903. recompile_reason:=rr_unknown;
  904. {
  905. The following fields should not
  906. be reset:
  907. mainsource
  908. state
  909. loaded_from
  910. sources_avail
  911. }
  912. end;
  913. procedure tmodule.loadlocalnamespacelist;
  914. var
  915. nsitem : TCmdStrListItem;
  916. begin
  917. // Copying local namespace list
  918. if premodule_namespacelist.Count>0 then
  919. begin
  920. nsitem:=TCmdStrListItem(premodule_namespacelist.First);
  921. while assigned(nsItem) do
  922. begin
  923. localnamespacelist.Concat(nsitem.Str);
  924. nsItem:=TCmdStrListItem(nsitem.Next);
  925. end;
  926. premodule_namespacelist.Clear;
  927. end;
  928. current_namespacelist:=localnamespacelist;
  929. end;
  930. procedure tmodule.adddependency(callermodule: tmodule; frominterface: boolean);
  931. begin
  932. { This is not needed for programs }
  933. if not callermodule.is_unit then
  934. exit;
  935. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  936. dependent_units.concat(tdependent_unit.create(callermodule,frominterface));
  937. end;
  938. procedure tmodule.flagdependent(callermodule:tmodule);
  939. var
  940. pm : tdependent_unit;
  941. m : tmodule;
  942. begin
  943. { flag all units that depend on this unit for reloading }
  944. pm:=tdependent_unit(current_module.dependent_units.first);
  945. while assigned(pm) do
  946. begin
  947. { We do not have to reload the unit that wants to load
  948. this unit, unless this unit is already compiled during
  949. the loading }
  950. m:=pm.u;
  951. if (m=callermodule) and (m.state<ms_compiled) then
  952. Message1(unit_u_no_reload_is_caller,m.modulename^)
  953. else
  954. if (m.state=ms_compile) {and (pm.u.compilecount>1)} then
  955. Message1(unit_u_no_reload_in_second_compile,m.modulename^)
  956. else
  957. begin
  958. m.do_reload:=true;
  959. Message1(unit_u_flag_for_reload,m.modulename^);
  960. end;
  961. pm:=tdependent_unit(pm.next);
  962. end;
  963. end;
  964. procedure tmodule.addimportedsym(sym:TSymEntry);
  965. begin
  966. if unitimportsyms.IndexOf(sym)<0 then
  967. unitimportsyms.Add(sym);
  968. end;
  969. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  970. var
  971. pu : tused_unit;
  972. begin
  973. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  974. used_units.concat(pu);
  975. addusedunit:=pu;
  976. end;
  977. function tmodule.usedunitsloaded(interface_units : boolean; out firstwaiting : tmodule): boolean;
  978. const
  979. statesneeded : array[boolean] of tmodulestates = ([ms_processed, ms_compiled,ms_compiling_waitimpl, ms_compiling_waitfinish],
  980. [ms_processed, ms_compiled,ms_compiling_waitimpl, ms_compiling_waitfinish]);
  981. var
  982. itm : TLinkedListItem;
  983. states : set of tmodulestate;
  984. begin
  985. Result:=True;
  986. States:=statesneeded[interface_units];
  987. itm:=self.used_units.First;
  988. firstwaiting:=Nil;
  989. while Result and assigned(itm) do
  990. begin
  991. result:=tused_unit(itm).u.state in states;
  992. {$IFDEF DEBUG_CTASK}writeln(' ',ToString,' checking state of ', tused_unit(itm).u.ToString,' : ',tused_unit(itm).u.state,' : ',Result);{$ENDIF}
  993. if not result then
  994. begin
  995. if firstwaiting=Nil then
  996. firstwaiting:=tused_unit(itm).u;
  997. end;
  998. itm:=itm.Next;
  999. end;
  1000. end;
  1001. function tmodule.nowaitingforunits(out firstwaiting : tmodule): Boolean;
  1002. begin
  1003. firstwaiting:=nil;
  1004. Result:=waitingforunit.count=0;
  1005. If not Result then
  1006. firstwaiting:=tmodule(waitingforunit[0]);
  1007. end;
  1008. function tmodule.usesmodule_in_interface(m: tmodule): boolean;
  1009. var
  1010. u : tused_unit;
  1011. begin
  1012. result:=False;
  1013. u:=tused_unit(used_units.First);
  1014. while assigned(u) do
  1015. begin
  1016. if (u.u=m) then
  1017. exit(u.in_interface) ;
  1018. u:=tused_unit(u.next);
  1019. end;
  1020. end;
  1021. function tmodule.findusedunit(m: tmodule): tused_unit;
  1022. var
  1023. u : tused_unit;
  1024. begin
  1025. result:=nil;
  1026. u:=tused_unit(used_units.First);
  1027. while assigned(u) do
  1028. begin
  1029. if (u.u=m) then
  1030. exit(u);
  1031. u:=tused_unit(u.next);
  1032. end;
  1033. end;
  1034. procedure tmodule.updatemaps;
  1035. var
  1036. oldmapsize : longint;
  1037. hp : tmodule;
  1038. i : longint;
  1039. begin
  1040. { Extend unitmap }
  1041. oldmapsize:=unitmapsize;
  1042. unitmapsize:=loaded_units.count;
  1043. setlength(unitmap,unitmapsize);
  1044. { Extend Derefmap }
  1045. oldmapsize:=derefmapsize;
  1046. derefmapsize:=loaded_units.count;
  1047. setlength(derefmap,derefmapsize);
  1048. { Add all units to unitmap }
  1049. hp:=tmodule(loaded_units.first);
  1050. i:=0;
  1051. while assigned(hp) do
  1052. begin
  1053. if hp.moduleid>=unitmapsize then
  1054. internalerror(200501151);
  1055. { Verify old entries }
  1056. if (i<oldmapsize) then
  1057. begin
  1058. if (hp.moduleid<>i) or
  1059. (unitmap[hp.moduleid].u<>hp) then
  1060. internalerror(200501156);
  1061. end
  1062. else
  1063. begin
  1064. unitmap[hp.moduleid].u:=hp;
  1065. unitmap[hp.moduleid].derefidx:=-1;
  1066. end;
  1067. inc(i);
  1068. hp:=tmodule(hp.next);
  1069. end;
  1070. end;
  1071. function tmodule.derefidx_unit(id:longint):longint;
  1072. begin
  1073. if id>=unitmapsize then
  1074. internalerror(2005011511);
  1075. if unitmap[id].derefidx=-1 then
  1076. begin
  1077. unitmap[id].derefidx:=derefmapcnt;
  1078. inc(derefmapcnt);
  1079. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  1080. end;
  1081. if unitmap[id].derefidx>=derefmapsize then
  1082. internalerror(2005011514);
  1083. result:=unitmap[id].derefidx;
  1084. end;
  1085. function tmodule.resolve_unit(id:longint):tmodule;
  1086. var
  1087. hp : tmodule;
  1088. begin
  1089. if id>=derefmapsize then
  1090. internalerror(200306231);
  1091. result:=derefmap[id].u;
  1092. if not assigned(result) then
  1093. begin
  1094. if not assigned(derefmap[id].modulename) or
  1095. (derefmap[id].modulename^='') then
  1096. internalerror(200501159);
  1097. hp:=tmodule(loaded_units.first);
  1098. while assigned(hp) do
  1099. begin
  1100. { only check for units. The main program is also
  1101. as a unit in the loaded_units list. We simply need
  1102. to ignore this entry (PFV) }
  1103. if hp.is_unit and
  1104. (hp.modulename^=derefmap[id].modulename^) then
  1105. break;
  1106. hp:=tmodule(hp.next);
  1107. end;
  1108. if not assigned(hp) then
  1109. internalerror(2005011510);
  1110. derefmap[id].u:=hp;
  1111. result:=hp;
  1112. end;
  1113. end;
  1114. procedure tmodule.allunitsused;
  1115. var
  1116. pu : tused_unit;
  1117. begin
  1118. pu:=tused_unit(used_units.first);
  1119. while assigned(pu) do
  1120. begin
  1121. if assigned(pu.u.globalsymtable) then
  1122. begin
  1123. if unitmap[pu.u.moduleid].u<>pu.u then
  1124. internalerror(200501157);
  1125. { Give a note when the unit is not referenced, skip
  1126. this is for units with an initialization/finalization }
  1127. if (unitmap[pu.u.moduleid].refs=0) and
  1128. pu.in_uses and
  1129. ((pu.u.moduleflags * [mf_init,mf_finalize])=[]) then
  1130. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  1131. end;
  1132. pu:=tused_unit(pu.next);
  1133. end;
  1134. end;
  1135. procedure tmodule.end_of_parsing;
  1136. begin
  1137. { free asmdata }
  1138. if assigned(asmdata) then
  1139. begin
  1140. asmdata.free;
  1141. asmdata:=nil;
  1142. end;
  1143. { free scanner }
  1144. if assigned(scanner) then
  1145. begin
  1146. if current_scanner=tscannerfile(scanner) then
  1147. set_current_scanner(nil);
  1148. FreeAndNil(scanner);
  1149. scanner:=nil;
  1150. end;
  1151. { free symtable stack }
  1152. if assigned(symtablestack) then
  1153. begin
  1154. symtablestack.free;
  1155. symtablestack:=nil;
  1156. end;
  1157. if assigned(macrosymtablestack) then
  1158. begin
  1159. macrosymtablestack.free;
  1160. macrosymtablestack:=nil;
  1161. end;
  1162. waitingforunit.free;
  1163. waitingforunit:=nil;
  1164. localmacrosymtable.free;
  1165. localmacrosymtable:=nil;
  1166. ptrdefs.free;
  1167. ptrdefs:=nil;
  1168. arraydefs.free;
  1169. arraydefs:=nil;
  1170. procaddrdefs.free;
  1171. procaddrdefs:=nil;
  1172. {$ifdef llvm}
  1173. llvmdefs.free;
  1174. llvmdefs:=nil;
  1175. {$endif llvm}
  1176. checkforwarddefs.free;
  1177. checkforwarddefs:=nil;
  1178. tcinitcode.free;
  1179. tcinitcode:=nil;
  1180. localunitsearchpath.free;
  1181. localunitsearchpath:=nil;
  1182. localobjectsearchpath.free;
  1183. localobjectsearchpath:=nil;
  1184. localincludesearchpath.free;
  1185. localincludesearchpath:=nil;
  1186. locallibrarysearchpath.free;
  1187. locallibrarysearchpath:=nil;
  1188. localframeworksearchpath.free;
  1189. localframeworksearchpath:=nil;
  1190. end;
  1191. procedure tmodule.setmodulename(const s:string);
  1192. begin
  1193. stringdispose(modulename);
  1194. stringdispose(realmodulename);
  1195. modulename:=stringdup(upper(s));
  1196. realmodulename:=stringdup(s);
  1197. { also update asmlibrary names }
  1198. current_asmdata.name:=modulename;
  1199. end;
  1200. procedure tmodule.AddExternalImport(const libname, symname, symmangledname: string; OrdNr: longint; isvar: boolean;
  1201. ImportByOrdinalOnly: boolean);
  1202. var
  1203. ImportLibrary,OtherIL : TImportLibrary;
  1204. ImportSymbol : TImportSymbol;
  1205. i : longint;
  1206. begin
  1207. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  1208. if not assigned(ImportLibrary) then
  1209. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  1210. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  1211. if not assigned(ImportSymbol) then
  1212. begin
  1213. { Check that the same name does not exist in another library }
  1214. { If it does and the same mangled name is used, issue a warning }
  1215. if ImportLibraryList.Count>1 then
  1216. for i:=0 To ImportLibraryList.Count-1 do
  1217. begin
  1218. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  1219. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  1220. if assigned(ImportSymbol) then
  1221. begin
  1222. if ImportSymbol.MangledName=symmangledname then
  1223. begin
  1224. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  1225. break;
  1226. end;
  1227. end;
  1228. end;
  1229. if not ImportByOrdinalOnly then
  1230. { negative ordinal number indicates import by name with ordinal number as hint }
  1231. OrdNr:=-OrdNr;
  1232. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  1233. symname,symmangledname,OrdNr,isvar);
  1234. end;
  1235. end;
  1236. procedure tmodule.add_public_asmsym(sym:TAsmSymbol);
  1237. begin
  1238. add_public_asmsym(sym.name,sym.bind,sym.typ);
  1239. end;
  1240. procedure tmodule.add_public_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1241. var
  1242. sym : tasmsymbol;
  1243. begin
  1244. { ToDo: check for AB_GLOBAL, AB_EXTERNAL? }
  1245. sym:=tasmsymbol(publicasmsyms.find(name));
  1246. if assigned(sym) then
  1247. begin
  1248. if (sym.bind<>bind) or (sym.typ<>typ) then
  1249. internalerror(2016070101);
  1250. exit;
  1251. end;
  1252. tasmsymbol.create(publicasmsyms,name,bind,typ);
  1253. end;
  1254. procedure tmodule.add_extern_asmsym(sym:TAsmSymbol);
  1255. begin
  1256. add_extern_asmsym(sym.name,sym.bind,sym.typ);
  1257. end;
  1258. procedure tmodule.add_extern_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1259. var
  1260. sym : tasmsymbol;
  1261. begin
  1262. { ToDo: check for AB_EXTERNAL? }
  1263. sym:=tasmsymbol(externasmsyms.find(name));
  1264. if assigned(sym) then
  1265. begin
  1266. if (sym.bind<>bind) or (sym.typ<>typ) then
  1267. internalerror(2016070102);
  1268. exit;
  1269. end;
  1270. tasmsymbol.create(externasmsyms,name,bind,typ);
  1271. end;
  1272. procedure tmodule.remove_from_waitingforunits(amodule: tmodule);
  1273. begin
  1274. // It can be nil after when this is called after end_of_parsing was called.
  1275. if assigned(waitingforunit) then
  1276. waitingforunit.remove(amodule);
  1277. end;
  1278. function tmodule.ToString: RTLString;
  1279. begin
  1280. // Assigned self so we can detect nil.
  1281. if assigned(modulename) then
  1282. Result:='('+ModuleName^+')'
  1283. else
  1284. Result:='(<'+inttostr(ptrint(self))+'>)';
  1285. // Possibly add some state ?
  1286. end;
  1287. initialization
  1288. {$ifdef MEMDEBUG}
  1289. memsymtable:=TMemDebug.create('Symtables');
  1290. memsymtable.stop;
  1291. {$endif MEMDEBUG}
  1292. finalization
  1293. {$ifdef MEMDEBUG}
  1294. memsymtable.free;
  1295. {$endif MEMDEBUG}
  1296. end.