fmodule.pas 47 KB

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