fmodule.pas 47 KB

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