fmodule.pas 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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(out firstwaiting : tmodule) : 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. procedure remove_from_waitingforunits(amodule : tmodule);
  245. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  246. function ToString: RTLString; override;
  247. end;
  248. tused_unit = class(tlinkedlistitem)
  249. checksum,
  250. interface_checksum,
  251. indirect_checksum: cardinal;
  252. in_uses,
  253. in_interface : boolean;
  254. u : tmodule;
  255. unitsym : tunitsym;
  256. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  257. procedure check_hints;
  258. end;
  259. tdependent_unit = class(tlinkedlistitem)
  260. u : tmodule;
  261. in_interface : boolean;
  262. constructor create(_u : tmodule; frominterface : boolean);
  263. end;
  264. var
  265. main_module : tmodule; { Main module of the program }
  266. current_module : tmodule; { Current module which is compiled or loaded }
  267. compiled_module : tmodule; { Current module which is compiled }
  268. usedunits : tlinkedlist; { Used units for this program }
  269. loaded_units : tlinkedlist; { All loaded units }
  270. unloaded_units : tlinkedlist; { Units removed from loaded_units, to be freed }
  271. SmartLinkOFiles : TCmdStrList; { List of .o files which are generated,
  272. used to delete them after linking }
  273. procedure set_current_module(p:tmodule);
  274. function get_module(moduleindex : longint) : tmodule;
  275. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  276. procedure addloadedunit(hp:tmodule);
  277. function find_module_from_symtable(st:tsymtable):tmodule;
  278. implementation
  279. uses
  280. SysUtils,globals,
  281. verbose,systems,
  282. scanner,ppu,dbgbase,
  283. procinfo,symdef,symtype;
  284. {$ifdef MEMDEBUG}
  285. var
  286. memsymtable : TMemDebug;
  287. {$endif}
  288. {*****************************************************************************
  289. Global Functions
  290. *****************************************************************************}
  291. function find_module_from_symtable(st:tsymtable):tmodule;
  292. var
  293. hp : tmodule;
  294. begin
  295. result:=nil;
  296. hp:=tmodule(loaded_units.first);
  297. while assigned(hp) do
  298. begin
  299. if (hp.moduleid=st.moduleid) then
  300. begin
  301. result:=hp;
  302. exit;
  303. end;
  304. hp:=tmodule(hp.next);
  305. end;
  306. end;
  307. procedure set_current_module(p:tmodule);
  308. begin
  309. { save the state of the scanner }
  310. if assigned(current_scanner) then
  311. current_scanner.tempcloseinputfile;
  312. { set new module }
  313. current_module:=p;
  314. { restore previous module settings }
  315. Fillchar(current_filepos,sizeof(current_filepos),0);
  316. if assigned(current_module) then
  317. begin
  318. current_asmdata:=tasmdata(current_module.asmdata);
  319. current_debuginfo:=tdebuginfo(current_module.debuginfo);
  320. { restore scanner and file positions }
  321. set_current_scanner(tscannerfile(current_module.scanner));
  322. if assigned(current_scanner) then
  323. begin
  324. current_scanner.tempopeninputfile;
  325. current_scanner.gettokenpos;
  326. parser_current_file:=current_scanner.inputfile.name;
  327. end
  328. else
  329. begin
  330. current_filepos.moduleindex:=current_module.unit_index;
  331. parser_current_file:='';
  332. end;
  333. end
  334. else
  335. begin
  336. current_asmdata:=nil;
  337. set_current_scanner(nil);
  338. current_debuginfo:=nil;
  339. end;
  340. end;
  341. function get_module(moduleindex : longint) : tmodule;
  342. var
  343. hp : tmodule;
  344. begin
  345. result:=nil;
  346. if moduleindex=0 then
  347. exit;
  348. result:=current_module;
  349. if not(assigned(loaded_units)) then
  350. exit;
  351. hp:=tmodule(loaded_units.first);
  352. while assigned(hp) and (hp.unit_index<>moduleindex) do
  353. hp:=tmodule(hp.next);
  354. result:=hp;
  355. end;
  356. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  357. var
  358. hp : tmodule;
  359. begin
  360. hp:=get_module(moduleindex);
  361. if assigned(hp) then
  362. get_source_file:=hp.sourcefiles.get_file(fileindex)
  363. else
  364. get_source_file:=nil;
  365. end;
  366. procedure addloadedunit(hp:tmodule);
  367. begin
  368. hp.moduleid:=loaded_units.count;
  369. loaded_units.concat(hp);
  370. end;
  371. {****************************************************************************
  372. TLinkContainerItem
  373. ****************************************************************************}
  374. constructor TLinkContainerItem.Create(const s:TPathStr;m:cardinal);
  375. begin
  376. inherited Create;
  377. data:=s;
  378. needlink:=m;
  379. end;
  380. {****************************************************************************
  381. TLinkContainer
  382. ****************************************************************************}
  383. procedure TLinkContainer.add(const s : TPathStr;m:cardinal);
  384. begin
  385. inherited concat(TLinkContainerItem.Create(s,m));
  386. end;
  387. function TLinkContainer.get(var m:cardinal) : TPathStr;
  388. var
  389. p : tlinkcontaineritem;
  390. begin
  391. p:=tlinkcontaineritem(inherited getfirst);
  392. if p=nil then
  393. begin
  394. get:='';
  395. m:=0;
  396. end
  397. else
  398. begin
  399. get:=p.data;
  400. m:=p.needlink;
  401. p.free;
  402. end;
  403. end;
  404. function TLinkContainer.getusemask(mask:cardinal) : TPathStr;
  405. var
  406. p : tlinkcontaineritem;
  407. found : boolean;
  408. begin
  409. found:=false;
  410. repeat
  411. p:=tlinkcontaineritem(inherited getfirst);
  412. if p=nil then
  413. begin
  414. getusemask:='';
  415. exit;
  416. end;
  417. getusemask:=p.data;
  418. found:=(p.needlink and mask)<>0;
  419. p.free;
  420. until found;
  421. end;
  422. function TLinkContainer.find(const s:TPathStr):boolean;
  423. var
  424. newnode : tlinkcontaineritem;
  425. begin
  426. find:=false;
  427. newnode:=tlinkcontaineritem(First);
  428. while assigned(newnode) do
  429. begin
  430. if newnode.data=s then
  431. begin
  432. find:=true;
  433. exit;
  434. end;
  435. newnode:=tlinkcontaineritem(newnode.next);
  436. end;
  437. end;
  438. {****************************************************************************
  439. TUSED_UNIT
  440. ****************************************************************************}
  441. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  442. begin
  443. u:=_u;
  444. in_interface:=intface;
  445. in_uses:=inuses;
  446. unitsym:=usym;
  447. if _u.state in [ms_compiled,ms_processed] then
  448. begin
  449. checksum:=u.crc;
  450. interface_checksum:=u.interface_crc;
  451. indirect_checksum:=u.indirect_crc;
  452. end
  453. else
  454. begin
  455. checksum:=0;
  456. interface_checksum:=0;
  457. indirect_checksum:=0;
  458. end;
  459. end;
  460. procedure tused_unit.check_hints;
  461. var
  462. uname: pshortstring;
  463. begin
  464. uname:=u.realmodulename;
  465. if mo_hint_deprecated in u.moduleoptions then
  466. if (mo_has_deprecated_msg in u.moduleoptions) and (u.deprecatedmsg <> nil) then
  467. MessagePos2(unitsym.fileinfo,sym_w_deprecated_unit_with_msg,uname^,u.deprecatedmsg^)
  468. else
  469. MessagePos1(unitsym.fileinfo,sym_w_deprecated_unit,uname^);
  470. if mo_hint_experimental in u.moduleoptions then
  471. MessagePos1(unitsym.fileinfo,sym_w_experimental_unit,uname^);
  472. if mo_hint_platform in u.moduleoptions then
  473. MessagePos1(unitsym.fileinfo,sym_w_non_portable_unit,uname^);
  474. if mo_hint_library in u.moduleoptions then
  475. MessagePos1(unitsym.fileinfo,sym_w_library_unit,uname^);
  476. if mo_hint_unimplemented in u.moduleoptions then
  477. MessagePos1(unitsym.fileinfo,sym_w_non_implemented_unit,uname^);
  478. end;
  479. {****************************************************************************
  480. TDENPENDENT_UNIT
  481. ****************************************************************************}
  482. constructor tdependent_unit.create(_u: tmodule; frominterface: boolean);
  483. begin
  484. u:=_u;
  485. in_interface:=frominterface;
  486. end;
  487. {****************************************************************************
  488. TMODULE
  489. ****************************************************************************}
  490. constructor tmodule.create(LoadedFrom:TModule;const amodulename: string; const afilename:TPathStr;_is_unit:boolean);
  491. var
  492. n:string;
  493. fn:TPathStr;
  494. begin
  495. if amodulename='' then
  496. n:=ChangeFileExt(ExtractFileName(afilename),'')
  497. else
  498. n:=amodulename;
  499. if afilename='' then
  500. fn:=amodulename
  501. else
  502. fn:=afilename;
  503. { Programs have the name 'Program' to don't conflict with dup id's }
  504. if _is_unit then
  505. inherited create(amodulename)
  506. else
  507. inherited create('Program');
  508. mainsource:=fn;
  509. { Dos has the famous 8.3 limit :( }
  510. {$ifdef shortasmprefix}
  511. asmprefix:=stringdup(FixFileName('as'));
  512. {$else}
  513. asmprefix:=stringdup(FixFileName(n));
  514. {$endif}
  515. setfilename(fn,true);
  516. localunitsearchpath:=TSearchPathList.Create;
  517. localobjectsearchpath:=TSearchPathList.Create;
  518. localincludesearchpath:=TSearchPathList.Create;
  519. locallibrarysearchpath:=TSearchPathList.Create;
  520. localframeworksearchpath:=TSearchPathList.Create;
  521. used_units:=TLinkedList.Create;
  522. dependent_units:=TLinkedList.Create;
  523. localnamespacelist:=TCmdStrList.Create;
  524. resourcefiles:=TCmdStrList.Create;
  525. linkorderedsymbols:=TCmdStrList.Create;
  526. linkunitofiles:=TLinkContainer.Create;
  527. linkunitstaticlibs:=TLinkContainer.Create;
  528. linkunitsharedlibs:=TLinkContainer.Create;
  529. linkotherofiles:=TLinkContainer.Create;
  530. linkotherstaticlibs:=TLinkContainer.Create;
  531. linkothersharedlibs:=TLinkContainer.Create;
  532. linkotherframeworks:=TLinkContainer.Create;
  533. mainname:=nil;
  534. FImportLibraryList:=TFPHashObjectList.Create(true);
  535. crc:=0;
  536. interface_crc:=0;
  537. indirect_crc:=0;
  538. headerflags:=0;
  539. longversion:=0;
  540. moduleflags:=[];
  541. scanner:=nil;
  542. unitmap:=nil;
  543. unitmapsize:=0;
  544. derefmap:=nil;
  545. derefmapsize:=0;
  546. derefmapcnt:=0;
  547. derefdata:=TDynamicArray.Create(1024);
  548. derefdataintflen:=0;
  549. deflist:=TFPObjectList.Create(false);
  550. symlist:=TFPObjectList.Create(false);
  551. ptrdefs:=THashSet.Create(64,true,false);
  552. arraydefs:=THashSet.Create(64,true,false);
  553. procaddrdefs:=THashSet.Create(64,true,false);
  554. {$ifdef llvm}
  555. llvmdefs:=THashSet.Create(64,true,false);
  556. llvmusedsyms:=TFPObjectList.Create(true);
  557. llvmcompilerusedsyms:=TFPObjectList.Create(true);
  558. llvminitprocs:=TFPList.Create;
  559. llvmfiniprocs:=TFPList.Create;
  560. llvmmetadatastrings:=TFPHashList.Create;
  561. {$endif llvm}
  562. ansistrdef:=nil;
  563. wpoinfo:=nil;
  564. checkforwarddefs:=TFPObjectList.Create(false);
  565. forwardgenericdefs:=TFPHashObjectList.Create(true);
  566. extendeddefs:=TFPHashObjectList.Create(true);
  567. genericdummysyms:=tfphashobjectlist.create(true);
  568. pendingspecializations:=tfphashobjectlist.create(false);
  569. waitingforunit:=tfpobjectlist.create(false);
  570. waitingunits:=tfpobjectlist.create(false);
  571. used_rtti_attrs:=tfpobjectlist.create(false);
  572. globalsymtable:=nil;
  573. localsymtable:=nil;
  574. globalmacrosymtable:=nil;
  575. localmacrosymtable:=nil;
  576. do_reload:=false;
  577. do_compile:=false;
  578. sources_avail:=true;
  579. mainfilepos.line:=0;
  580. mainfilepos.column:=0;
  581. mainfilepos.fileindex:=0;
  582. recompile_reason:=rr_unknown;
  583. in_interface:=true;
  584. in_global:=true;
  585. is_unit:=_is_unit;
  586. islibrary:=false;
  587. ispackage:=false;
  588. change_endian:=false;
  589. is_dbginfo_written:=false;
  590. mode_switch_allowed:= true;
  591. moduleoptions:=[];
  592. deprecatedmsg:=nil;
  593. namespace:=nil;
  594. tcinitcode:=nil;
  595. _exports:=TLinkedList.Create;
  596. dllscannerinputlist:=TFPHashList.Create;
  597. asmdata:=casmdata.create(modulename);
  598. unitimportsyms:=TFPObjectList.Create(false);
  599. publicasmsyms:=TFPHashObjectList.Create(true);
  600. externasmsyms:=TFPHashObjectList.Create(true);
  601. InitDebugInfo(self,false);
  602. end;
  603. destructor tmodule.destroy;
  604. var
  605. i : longint;
  606. current_debuginfo_reset : boolean;
  607. begin
  608. if assigned(unitmap) then
  609. freemem(unitmap);
  610. if assigned(derefmap) then
  611. begin
  612. for i:=0 to derefmapcnt-1 do
  613. stringdispose(derefmap[i].modulename);
  614. freemem(derefmap);
  615. end;
  616. if assigned(_exports) then
  617. _exports.free;
  618. if assigned(dllscannerinputlist) then
  619. dllscannerinputlist.free;
  620. if assigned(scanner) then
  621. begin
  622. { also update current_scanner if it was pointing
  623. to this module }
  624. if current_scanner=tscannerfile(scanner) then
  625. set_current_scanner(nil);
  626. freeandnil(scanner);
  627. end;
  628. if assigned(asmdata) then
  629. begin
  630. if current_asmdata=asmdata then
  631. current_asmdata:=nil;
  632. asmdata.free;
  633. end;
  634. if assigned(procinfo) then
  635. begin
  636. if current_procinfo=tprocinfo(procinfo) then
  637. begin
  638. current_procinfo:=nil;
  639. current_structdef:=nil;
  640. current_genericdef:=nil;
  641. current_specializedef:=nil;
  642. end;
  643. { release procinfo tree }
  644. tprocinfo(procinfo).destroy_tree;
  645. end;
  646. DoneDebugInfo(self,current_debuginfo_reset);
  647. used_units.free;
  648. dependent_units.free;
  649. resourcefiles.Free;
  650. linkorderedsymbols.Free;
  651. linkunitofiles.Free;
  652. linkunitstaticlibs.Free;
  653. linkunitsharedlibs.Free;
  654. linkotherofiles.Free;
  655. linkotherstaticlibs.Free;
  656. linkothersharedlibs.Free;
  657. linkotherframeworks.Free;
  658. stringdispose(mainname);
  659. externasmsyms.Free;
  660. publicasmsyms.Free;
  661. unitimportsyms.Free;
  662. FImportLibraryList.Free;
  663. extendeddefs.Free;
  664. genericdummysyms.free;
  665. pendingspecializations.free;
  666. waitingforunit.free;
  667. waitingunits.free;
  668. used_rtti_attrs.free;
  669. stringdispose(asmprefix);
  670. stringdispose(deprecatedmsg);
  671. stringdispose(namespace);
  672. tcinitcode.free;
  673. localunitsearchpath.Free;
  674. localobjectsearchpath.free;
  675. localincludesearchpath.free;
  676. locallibrarysearchpath.free;
  677. localframeworksearchpath.free;
  678. {$ifdef MEMDEBUG}
  679. memsymtable.start;
  680. {$endif}
  681. derefdata.free;
  682. if assigned(deflist) then
  683. begin
  684. for i:=0 to deflist.Count-1 do
  685. if assigned(deflist[i]) and
  686. (tdef(deflist[i]).registered_in_module=self) then
  687. tdef(deflist[i]).registered_in_module:=nil;
  688. deflist.free;
  689. end;
  690. symlist.free;
  691. ptrdefs.free;
  692. arraydefs.free;
  693. procaddrdefs.free;
  694. {$ifdef llvm}
  695. llvmdefs.free;
  696. llvmusedsyms.free;
  697. llvmcompilerusedsyms.free;
  698. llvminitprocs.free;
  699. llvmfiniprocs.free;
  700. llvmmetadatastrings.free;
  701. {$endif llvm}
  702. ansistrdef:=nil;
  703. wpoinfo.free;
  704. checkforwarddefs.free;
  705. forwardgenericdefs.free;
  706. globalsymtable.free;
  707. localsymtable.free;
  708. globalmacrosymtable.free;
  709. localmacrosymtable.free;
  710. {$ifdef MEMDEBUG}
  711. memsymtable.stop;
  712. {$endif}
  713. inherited Destroy;
  714. end;
  715. procedure tmodule.reset;
  716. var
  717. i : longint;
  718. current_debuginfo_reset : boolean;
  719. begin
  720. is_reset:=true;
  721. if assigned(scanner) then
  722. begin
  723. { also update current_scanner if it was pointing
  724. to this module }
  725. if current_scanner=tscannerfile(scanner) then
  726. set_current_scanner(nil);
  727. freeandnil(scanner);
  728. end;
  729. if assigned(procinfo) then
  730. begin
  731. if current_procinfo=tprocinfo(procinfo) then
  732. begin
  733. current_procinfo:=nil;
  734. current_structdef:=nil;
  735. current_genericdef:=nil;
  736. current_specializedef:=nil;
  737. end;
  738. { release procinfo tree }
  739. tprocinfo(procinfo).destroy_tree;
  740. end;
  741. if assigned(asmdata) then
  742. begin
  743. if current_asmdata=asmdata then
  744. current_asmdata:=nil;
  745. asmdata.free;
  746. asmdata:=nil;
  747. end;
  748. DoneDebugInfo(self,current_debuginfo_reset);
  749. globalsymtable.free;
  750. globalsymtable:=nil;
  751. localsymtable.free;
  752. localsymtable:=nil;
  753. globalmacrosymtable.free;
  754. globalmacrosymtable:=nil;
  755. localmacrosymtable.free;
  756. localmacrosymtable:=nil;
  757. deflist.free;
  758. deflist:=TFPObjectList.Create(false);
  759. symlist.free;
  760. symlist:=TFPObjectList.Create(false);
  761. ptrdefs.free;
  762. ptrdefs:=THashSet.Create(64,true,false);
  763. arraydefs.free;
  764. arraydefs:=THashSet.Create(64,true,false);
  765. procaddrdefs.free;
  766. procaddrdefs:=THashSet.Create(64,true,false);
  767. {$ifdef llvm}
  768. llvmdefs.free;
  769. llvmdefs:=THashSet.Create(64,true,false);
  770. llvmusedsyms.free;
  771. llvmusedsyms:=TFPObjectList.Create(true);
  772. llvmcompilerusedsyms.free;
  773. llvmcompilerusedsyms:=TFPObjectList.Create(true);
  774. llvminitprocs.free;
  775. llvminitprocs:=TFPList.Create;
  776. llvmfiniprocs.free;
  777. llvmfiniprocs:=TFPList.Create;
  778. llvmmetadatastrings.free;
  779. llvmmetadatastrings:=TFPHashList.Create;
  780. {$endif llvm}
  781. wpoinfo.free;
  782. wpoinfo:=nil;
  783. checkforwarddefs.free;
  784. checkforwarddefs:=TFPObjectList.Create(false);
  785. forwardgenericdefs.free;
  786. forwardgenericdefs:=TFPHashObjectList.Create(true);
  787. publicasmsyms.free;
  788. publicasmsyms:=TFPHashObjectList.Create(true);
  789. externasmsyms.free;
  790. externasmsyms:=TFPHashObjectList.Create(true);
  791. unitimportsyms.free;
  792. unitimportsyms:=TFPObjectList.Create(false);
  793. derefdata.free;
  794. derefdata:=TDynamicArray.Create(1024);
  795. if assigned(unitmap) then
  796. begin
  797. freemem(unitmap);
  798. unitmap:=nil;
  799. end;
  800. if assigned(derefmap) then
  801. begin
  802. for i:=0 to derefmapcnt-1 do
  803. stringdispose(derefmap[i].modulename);
  804. freemem(derefmap);
  805. derefmap:=nil;
  806. end;
  807. unitmapsize:=0;
  808. derefmapsize:=0;
  809. derefmapcnt:=0;
  810. derefdataintflen:=0;
  811. sourcefiles.free;
  812. sourcefiles:=tinputfilemanager.create;
  813. asmdata:=casmdata.create(modulename);
  814. InitDebugInfo(self,current_debuginfo_reset);
  815. _exports.free;
  816. _exports:=tlinkedlist.create;
  817. dllscannerinputlist.free;
  818. dllscannerinputlist:=TFPHashList.create;
  819. used_units.free;
  820. used_units:=TLinkedList.Create;
  821. dependent_units.free;
  822. dependent_units:=TLinkedList.Create;
  823. resourcefiles.Free;
  824. resourcefiles:=TCmdStrList.Create;
  825. linkorderedsymbols.Free;
  826. linkorderedsymbols:=TCmdStrList.Create;
  827. pendingspecializations.free;
  828. pendingspecializations:=tfphashobjectlist.create(false);
  829. if assigned(waitingforunit) and
  830. (waitingforunit.count<>0) then
  831. internalerror(2016070501);
  832. waitingforunit.free;
  833. waitingforunit:=tfpobjectlist.create(false);
  834. linkunitofiles.Free;
  835. linkunitofiles:=TLinkContainer.Create;
  836. linkunitstaticlibs.Free;
  837. linkunitstaticlibs:=TLinkContainer.Create;
  838. linkunitsharedlibs.Free;
  839. linkunitsharedlibs:=TLinkContainer.Create;
  840. linkotherofiles.Free;
  841. linkotherofiles:=TLinkContainer.Create;
  842. linkotherstaticlibs.Free;
  843. linkotherstaticlibs:=TLinkContainer.Create;
  844. linkothersharedlibs.Free;
  845. linkothersharedlibs:=TLinkContainer.Create;
  846. linkotherframeworks.Free;
  847. linkotherframeworks:=TLinkContainer.Create;
  848. stringdispose(mainname);
  849. FImportLibraryList.Free;
  850. FImportLibraryList:=TFPHashObjectList.Create;
  851. do_compile:=false;
  852. do_reload:=false;
  853. interface_compiled:=false;
  854. in_interface:=true;
  855. in_global:=true;
  856. mode_switch_allowed:=true;
  857. stringdispose(deprecatedmsg);
  858. stringdispose(namespace);
  859. tcinitcode.free;
  860. tcinitcode:=nil;
  861. localunitsearchpath.Free;
  862. localunitsearchpath:=TSearchPathList.Create;
  863. localobjectsearchpath.free;
  864. localobjectsearchpath:=TSearchPathList.Create;
  865. localincludesearchpath.free;
  866. localincludesearchpath:=TSearchPathList.Create;
  867. locallibrarysearchpath.free;
  868. locallibrarysearchpath:=TSearchPathList.Create;
  869. localframeworksearchpath.free;
  870. localframeworksearchpath:=TSearchPathList.Create;
  871. moduleoptions:=[];
  872. is_dbginfo_written:=false;
  873. crc:=0;
  874. interface_crc:=0;
  875. indirect_crc:=0;
  876. headerflags:=0;
  877. longversion:=0;
  878. moduleflags:=[];
  879. mainfilepos.line:=0;
  880. mainfilepos.column:=0;
  881. mainfilepos.fileindex:=0;
  882. recompile_reason:=rr_unknown;
  883. {
  884. The following fields should not
  885. be reset:
  886. mainsource
  887. state
  888. loaded_from
  889. sources_avail
  890. }
  891. end;
  892. procedure tmodule.loadlocalnamespacelist;
  893. var
  894. nsitem : TCmdStrListItem;
  895. begin
  896. // Copying local namespace list
  897. if premodule_namespacelist.Count>0 then
  898. begin
  899. nsitem:=TCmdStrListItem(premodule_namespacelist.First);
  900. while assigned(nsItem) do
  901. begin
  902. localnamespacelist.Concat(nsitem.Str);
  903. nsItem:=TCmdStrListItem(nsitem.Next);
  904. end;
  905. premodule_namespacelist.Clear;
  906. end;
  907. current_namespacelist:=localnamespacelist;
  908. end;
  909. procedure tmodule.adddependency(callermodule: tmodule; frominterface: boolean);
  910. begin
  911. { This is not needed for programs }
  912. if not callermodule.is_unit then
  913. exit;
  914. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  915. dependent_units.concat(tdependent_unit.create(callermodule,frominterface));
  916. end;
  917. procedure tmodule.flagdependent(callermodule:tmodule);
  918. var
  919. pm : tdependent_unit;
  920. begin
  921. { flag all units that depend on this unit for reloading }
  922. pm:=tdependent_unit(current_module.dependent_units.first);
  923. while assigned(pm) do
  924. begin
  925. { We do not have to reload the unit that wants to load
  926. this unit, unless this unit is already compiled during
  927. the loading }
  928. if (pm.u=callermodule) and
  929. (pm.u.state<ms_compiled) then
  930. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  931. else
  932. if (pm.u.state=ms_compile) and (pm.u.compilecount>1) then
  933. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  934. else
  935. begin
  936. pm.u.do_reload:=true;
  937. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  938. end;
  939. pm:=tdependent_unit(pm.next);
  940. end;
  941. end;
  942. procedure tmodule.addimportedsym(sym:TSymEntry);
  943. begin
  944. if unitimportsyms.IndexOf(sym)<0 then
  945. unitimportsyms.Add(sym);
  946. end;
  947. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  948. var
  949. pu : tused_unit;
  950. begin
  951. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  952. used_units.concat(pu);
  953. addusedunit:=pu;
  954. end;
  955. function tmodule.usedunitsloaded(interface_units : boolean; out firstwaiting : tmodule): boolean;
  956. const
  957. statesneeded : array[boolean] of tmodulestates = ([ms_processed, ms_compiled,ms_compiling_waitimpl, ms_compiling_waitfinish],
  958. [ms_processed, ms_compiled,ms_compiling_waitimpl, ms_compiling_waitfinish]);
  959. var
  960. itm : TLinkedListItem;
  961. states : set of tmodulestate;
  962. begin
  963. Result:=True;
  964. States:=statesneeded[interface_units];
  965. itm:=self.used_units.First;
  966. firstwaiting:=Nil;
  967. while Result and assigned(itm) do
  968. begin
  969. result:=tused_unit(itm).u.state in states;
  970. {$IFDEF DEBUG_CTASK}writeln(' ',ToString,' checking state of ', tused_unit(itm).u.ToString,' : ',tused_unit(itm).u.state,' : ',Result);{$ENDIF}
  971. if not result then
  972. begin
  973. if firstwaiting=Nil then
  974. firstwaiting:=tused_unit(itm).u;
  975. end;
  976. itm:=itm.Next;
  977. end;
  978. end;
  979. function tmodule.nowaitingforunits(out firstwaiting : tmodule): Boolean;
  980. begin
  981. firstwaiting:=nil;
  982. Result:=waitingforunit.count=0;
  983. If not Result then
  984. firstwaiting:=tmodule(waitingforunit[0]);
  985. end;
  986. function tmodule.usesmodule_in_interface(m: tmodule): boolean;
  987. var
  988. u : tused_unit;
  989. begin
  990. result:=False;
  991. u:=tused_unit(used_units.First);
  992. while assigned(u) do
  993. begin
  994. if (u.u=m) then
  995. exit(u.in_interface) ;
  996. u:=tused_unit(u.next);
  997. end;
  998. end;
  999. procedure tmodule.updatemaps;
  1000. var
  1001. oldmapsize : longint;
  1002. hp : tmodule;
  1003. i : longint;
  1004. begin
  1005. { Extend unitmap }
  1006. oldmapsize:=unitmapsize;
  1007. unitmapsize:=loaded_units.count;
  1008. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  1009. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  1010. { Extend Derefmap }
  1011. oldmapsize:=derefmapsize;
  1012. derefmapsize:=loaded_units.count;
  1013. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  1014. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  1015. { Add all units to unitmap }
  1016. hp:=tmodule(loaded_units.first);
  1017. i:=0;
  1018. while assigned(hp) do
  1019. begin
  1020. if hp.moduleid>=unitmapsize then
  1021. internalerror(200501151);
  1022. { Verify old entries }
  1023. if (i<oldmapsize) then
  1024. begin
  1025. if (hp.moduleid<>i) or
  1026. (unitmap[hp.moduleid].u<>hp) then
  1027. internalerror(200501156);
  1028. end
  1029. else
  1030. begin
  1031. unitmap[hp.moduleid].u:=hp;
  1032. unitmap[hp.moduleid].derefidx:=-1;
  1033. end;
  1034. inc(i);
  1035. hp:=tmodule(hp.next);
  1036. end;
  1037. end;
  1038. function tmodule.derefidx_unit(id:longint):longint;
  1039. begin
  1040. if id>=unitmapsize then
  1041. internalerror(2005011511);
  1042. if unitmap[id].derefidx=-1 then
  1043. begin
  1044. unitmap[id].derefidx:=derefmapcnt;
  1045. inc(derefmapcnt);
  1046. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  1047. end;
  1048. if unitmap[id].derefidx>=derefmapsize then
  1049. internalerror(2005011514);
  1050. result:=unitmap[id].derefidx;
  1051. end;
  1052. function tmodule.resolve_unit(id:longint):tmodule;
  1053. var
  1054. hp : tmodule;
  1055. begin
  1056. if id>=derefmapsize then
  1057. internalerror(200306231);
  1058. result:=derefmap[id].u;
  1059. if not assigned(result) then
  1060. begin
  1061. if not assigned(derefmap[id].modulename) or
  1062. (derefmap[id].modulename^='') then
  1063. internalerror(200501159);
  1064. hp:=tmodule(loaded_units.first);
  1065. while assigned(hp) do
  1066. begin
  1067. { only check for units. The main program is also
  1068. as a unit in the loaded_units list. We simply need
  1069. to ignore this entry (PFV) }
  1070. if hp.is_unit and
  1071. (hp.modulename^=derefmap[id].modulename^) then
  1072. break;
  1073. hp:=tmodule(hp.next);
  1074. end;
  1075. if not assigned(hp) then
  1076. internalerror(2005011510);
  1077. derefmap[id].u:=hp;
  1078. result:=hp;
  1079. end;
  1080. end;
  1081. procedure tmodule.allunitsused;
  1082. var
  1083. pu : tused_unit;
  1084. begin
  1085. pu:=tused_unit(used_units.first);
  1086. while assigned(pu) do
  1087. begin
  1088. if assigned(pu.u.globalsymtable) then
  1089. begin
  1090. if unitmap[pu.u.moduleid].u<>pu.u then
  1091. internalerror(200501157);
  1092. { Give a note when the unit is not referenced, skip
  1093. this is for units with an initialization/finalization }
  1094. if (unitmap[pu.u.moduleid].refs=0) and
  1095. pu.in_uses and
  1096. ((pu.u.moduleflags * [mf_init,mf_finalize])=[]) then
  1097. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  1098. end;
  1099. pu:=tused_unit(pu.next);
  1100. end;
  1101. end;
  1102. procedure tmodule.end_of_parsing;
  1103. begin
  1104. { free asmdata }
  1105. if assigned(asmdata) then
  1106. begin
  1107. asmdata.free;
  1108. asmdata:=nil;
  1109. end;
  1110. { free scanner }
  1111. if assigned(scanner) then
  1112. begin
  1113. if current_scanner=tscannerfile(scanner) then
  1114. set_current_scanner(nil);
  1115. FreeAndNil(scanner);
  1116. scanner:=nil;
  1117. end;
  1118. { free symtable stack }
  1119. if assigned(symtablestack) then
  1120. begin
  1121. symtablestack.free;
  1122. symtablestack:=nil;
  1123. end;
  1124. if assigned(macrosymtablestack) then
  1125. begin
  1126. macrosymtablestack.free;
  1127. macrosymtablestack:=nil;
  1128. end;
  1129. waitingforunit.free;
  1130. waitingforunit:=nil;
  1131. localmacrosymtable.free;
  1132. localmacrosymtable:=nil;
  1133. ptrdefs.free;
  1134. ptrdefs:=nil;
  1135. arraydefs.free;
  1136. arraydefs:=nil;
  1137. procaddrdefs.free;
  1138. procaddrdefs:=nil;
  1139. {$ifdef llvm}
  1140. llvmdefs.free;
  1141. llvmdefs:=nil;
  1142. {$endif llvm}
  1143. checkforwarddefs.free;
  1144. checkforwarddefs:=nil;
  1145. tcinitcode.free;
  1146. tcinitcode:=nil;
  1147. localunitsearchpath.free;
  1148. localunitsearchpath:=nil;
  1149. localobjectsearchpath.free;
  1150. localobjectsearchpath:=nil;
  1151. localincludesearchpath.free;
  1152. localincludesearchpath:=nil;
  1153. locallibrarysearchpath.free;
  1154. locallibrarysearchpath:=nil;
  1155. localframeworksearchpath.free;
  1156. localframeworksearchpath:=nil;
  1157. end;
  1158. procedure tmodule.setmodulename(const s:string);
  1159. begin
  1160. stringdispose(modulename);
  1161. stringdispose(realmodulename);
  1162. modulename:=stringdup(upper(s));
  1163. realmodulename:=stringdup(s);
  1164. { also update asmlibrary names }
  1165. current_asmdata.name:=modulename;
  1166. end;
  1167. procedure tmodule.AddExternalImport(const libname, symname, symmangledname: string; OrdNr: longint; isvar: boolean;
  1168. ImportByOrdinalOnly: boolean);
  1169. var
  1170. ImportLibrary,OtherIL : TImportLibrary;
  1171. ImportSymbol : TImportSymbol;
  1172. i : longint;
  1173. begin
  1174. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  1175. if not assigned(ImportLibrary) then
  1176. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  1177. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  1178. if not assigned(ImportSymbol) then
  1179. begin
  1180. { Check that the same name does not exist in another library }
  1181. { If it does and the same mangled name is used, issue a warning }
  1182. if ImportLibraryList.Count>1 then
  1183. for i:=0 To ImportLibraryList.Count-1 do
  1184. begin
  1185. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  1186. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  1187. if assigned(ImportSymbol) then
  1188. begin
  1189. if ImportSymbol.MangledName=symmangledname then
  1190. begin
  1191. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  1192. break;
  1193. end;
  1194. end;
  1195. end;
  1196. if not ImportByOrdinalOnly then
  1197. { negative ordinal number indicates import by name with ordinal number as hint }
  1198. OrdNr:=-OrdNr;
  1199. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  1200. symname,symmangledname,OrdNr,isvar);
  1201. end;
  1202. end;
  1203. procedure tmodule.add_public_asmsym(sym:TAsmSymbol);
  1204. begin
  1205. add_public_asmsym(sym.name,sym.bind,sym.typ);
  1206. end;
  1207. procedure tmodule.add_public_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1208. var
  1209. sym : tasmsymbol;
  1210. begin
  1211. { ToDo: check for AB_GLOBAL, AB_EXTERNAL? }
  1212. sym:=tasmsymbol(publicasmsyms.find(name));
  1213. if assigned(sym) then
  1214. begin
  1215. if (sym.bind<>bind) or (sym.typ<>typ) then
  1216. internalerror(2016070101);
  1217. exit;
  1218. end;
  1219. tasmsymbol.create(publicasmsyms,name,bind,typ);
  1220. end;
  1221. procedure tmodule.add_extern_asmsym(sym:TAsmSymbol);
  1222. begin
  1223. add_extern_asmsym(sym.name,sym.bind,sym.typ);
  1224. end;
  1225. procedure tmodule.add_extern_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1226. var
  1227. sym : tasmsymbol;
  1228. begin
  1229. { ToDo: check for AB_EXTERNAL? }
  1230. sym:=tasmsymbol(externasmsyms.find(name));
  1231. if assigned(sym) then
  1232. begin
  1233. if (sym.bind<>bind) or (sym.typ<>typ) then
  1234. internalerror(2016070102);
  1235. exit;
  1236. end;
  1237. tasmsymbol.create(externasmsyms,name,bind,typ);
  1238. end;
  1239. procedure tmodule.remove_from_waitingforunits(amodule: tmodule);
  1240. begin
  1241. // It can be nil after when this is called after end_of_parsing was called.
  1242. if assigned(waitingforunit) then
  1243. waitingforunit.remove(amodule);
  1244. end;
  1245. function tmodule.ToString: RTLString;
  1246. begin
  1247. // Assigned self so we can detect nil.
  1248. if assigned(modulename) then
  1249. Result:='('+ModuleName^+')'
  1250. else
  1251. Result:='(<'+inttostr(ptrint(self))+'>)';
  1252. // Possibly add some state ?
  1253. end;
  1254. initialization
  1255. {$ifdef MEMDEBUG}
  1256. memsymtable:=TMemDebug.create('Symtables');
  1257. memsymtable.stop;
  1258. {$endif MEMDEBUG}
  1259. finalization
  1260. {$ifdef MEMDEBUG}
  1261. memsymtable.free;
  1262. {$endif MEMDEBUG}
  1263. end.