fmodule.pas 49 KB

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