fmodule.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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 tos}
  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,symconst,symsym,
  39. wpobase,
  40. aasmbase,aasmtai,aasmdata;
  41. const
  42. UNSPECIFIED_LIBRARY_NAME = '<none>';
  43. type
  44. trecompile_reason = (rr_unknown,
  45. rr_noppu,rr_sourcenewer,rr_build,rr_crcchanged
  46. );
  47. { unit options }
  48. tmoduleoption = (mo_none,
  49. mo_hint_deprecated,
  50. mo_hint_platform,
  51. mo_hint_library,
  52. mo_hint_unimplemented,
  53. mo_hint_experimental,
  54. mo_has_deprecated_msg
  55. );
  56. tmoduleoptions = set of tmoduleoption;
  57. tlinkcontaineritem=class(tlinkedlistitem)
  58. public
  59. data : TPathStr;
  60. needlink : cardinal;
  61. constructor Create(const s:TPathStr;m:cardinal);
  62. end;
  63. tlinkcontainer=class(tlinkedlist)
  64. procedure add(const s : TPathStr;m:cardinal);
  65. function get(var m:cardinal) : TPathStr;
  66. function getusemask(mask:cardinal) : TPathStr;
  67. function find(const s:TPathStr):boolean;
  68. end;
  69. tmodule = class;
  70. tused_unit = class;
  71. tunitmaprec = record
  72. u : tmodule;
  73. { number of references }
  74. refs : longint;
  75. { index in the derefmap }
  76. derefidx : longint;
  77. end;
  78. punitmap = ^tunitmaprec;
  79. tderefmaprec = record
  80. u : tmodule;
  81. { modulename, used during ppu load }
  82. modulename : pshortstring;
  83. end;
  84. pderefmap = ^tderefmaprec;
  85. { tmodule }
  86. tmodule = class(tmodulebase)
  87. private
  88. FImportLibraryList : TFPHashObjectList;
  89. public
  90. do_reload, { force reloading of the unit }
  91. do_compile, { need to compile the sources }
  92. sources_avail, { if all sources are reachable }
  93. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  94. is_dbginfo_written,
  95. is_unit,
  96. in_interface, { processing the implementation part? }
  97. { allow global settings }
  98. in_global : boolean;
  99. { Whether a mode switch is still allowed at this point in the parsing.}
  100. mode_switch_allowed,
  101. { generate pic helper which loads eip in ecx (for leave procedures) }
  102. requires_ecx_pic_helper,
  103. { generate pic helper which loads eip in ebx (for non leave procedures) }
  104. requires_ebx_pic_helper : boolean;
  105. interface_only: boolean; { interface-only macpas unit; flag does not need saving/restoring to ppu }
  106. mainfilepos : tfileposinfo;
  107. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  108. crc,
  109. interface_crc,
  110. indirect_crc : cardinal;
  111. flags : cardinal; { the PPU flags }
  112. islibrary : boolean; { if it is a library (win32 dll) }
  113. IsPackage : boolean;
  114. moduleid : longint;
  115. unitmap : punitmap; { mapping of all used units }
  116. unitmapsize : longint; { number of units in the map }
  117. derefmap : pderefmap; { mapping of all units needed for deref }
  118. derefmapcnt : longint; { number of units in the map }
  119. derefmapsize : longint; { number of units in the map }
  120. derefdataintflen : longint;
  121. derefdata : tdynamicarray;
  122. checkforwarddefs,
  123. deflist,
  124. symlist : TFPObjectList;
  125. ptrdefs : THashSet; { list of pointerdefs created in this module so we can reuse them (not saved/restored) }
  126. arraydefs : THashSet; { list of single-element-arraydefs created in this module so we can reuse them (not saved/restored) }
  127. procaddrdefs : THashSet; { list of procvardefs created when getting the address of a procdef (not saved/restored) }
  128. {$ifdef llvm}
  129. llvmdefs : THashSet; { defs added for llvm-specific reasons (not saved/restored) }
  130. {$endif llvm}
  131. ansistrdef : tobject; { an ansistring def redefined for the current module }
  132. wpoinfo : tunitwpoinfobase; { whole program optimization-related information that is generated during the current run for this unit }
  133. globalsymtable, { pointer to the global symtable of this unit }
  134. localsymtable : TSymtable;{ pointer to the local symtable of this unit }
  135. globalmacrosymtable, { pointer to the global macro symtable of this unit }
  136. localmacrosymtable : TSymtable;{ pointer to the local macro symtable of this unit }
  137. scanner : TObject; { scanner object used }
  138. procinfo : TObject; { current procedure being compiled }
  139. asmdata : TObject; { Assembler data }
  140. asmprefix : pshortstring; { prefix for the smartlink asmfiles }
  141. unitimportsyms : tfpobjectlist; { list of symbols that are imported from other units }
  142. debuginfo : TObject;
  143. loaded_from : tmodule;
  144. _exports : tlinkedlist;
  145. dllscannerinputlist : TFPHashList;
  146. resourcefiles : TCmdStrList;
  147. linkunitofiles,
  148. linkunitstaticlibs,
  149. linkunitsharedlibs,
  150. linkotherofiles, { objects,libs loaded from the source }
  151. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  152. linkotherstaticlibs,
  153. linkotherframeworks : tlinkcontainer;
  154. mainname : pshortstring; { alternate name for "main" procedure }
  155. package : tpackage;
  156. used_units : tlinkedlist;
  157. dependent_units : tlinkedlist;
  158. localunitsearchpath, { local searchpaths }
  159. localobjectsearchpath,
  160. localincludesearchpath,
  161. locallibrarysearchpath,
  162. localframeworksearchpath : TSearchPathList;
  163. moduleoptions: tmoduleoptions;
  164. deprecatedmsg: pshortstring;
  165. { contains a list of types that are extended by helper types; the key is
  166. the full name of the type and the data is a TFPObjectList of
  167. tobjectdef instances (the helper defs) }
  168. extendeddefs: TFPHashObjectList;
  169. { contains a list of the current topmost non-generic symbol for a
  170. typename of which at least one generic exists; the key is the
  171. non-generic typename and the data is a TFPObjectList of tgenericdummyentry
  172. instances whereby the last one is the current top most one }
  173. genericdummysyms: TFPHashObjectList;
  174. { this contains a list of units that needs to be waited for until the
  175. unit can be finished (code generated, etc.); this is needed to handle
  176. specializations in circular unit usages correctly }
  177. waitingforunit: tfpobjectlist;
  178. { this contains a list of all units that are waiting for this unit to be
  179. finished }
  180. waitingunits: tfpobjectlist;
  181. finishstate: pointer;
  182. globalstate: pointer;
  183. namespace: pshortstring; { for JVM target: corresponds to Java package name }
  184. { for targets that initialise typed constants via explicit assignments
  185. instead of by generating an initialised data section (holds typed
  186. constant assignments at the module level; does not have to be saved
  187. into the ppu file, because translated into code during compilation)
  188. -- actual type: tnode (but fmodule should not depend on node) }
  189. tcinitcode : tobject;
  190. {create creates a new module which name is stored in 's'. LoadedFrom
  191. points to the module calling it. It is nil for the first compiled
  192. module. This allow inheritence of all path lists. MUST pay attention
  193. to that when creating link.res!!!!(mazen)}
  194. constructor create(LoadedFrom:TModule;const amodulename: string; const afilename:TPathStr;_is_unit:boolean);
  195. destructor destroy;override;
  196. procedure reset;virtual;
  197. procedure adddependency(callermodule:tmodule);
  198. procedure flagdependent(callermodule:tmodule);
  199. procedure addimportedsym(sym:TSymEntry);
  200. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  201. procedure updatemaps;
  202. function derefidx_unit(id:longint):longint;
  203. function resolve_unit(id:longint):tmodule;
  204. procedure allunitsused;
  205. procedure end_of_parsing;virtual;
  206. procedure setmodulename(const s:string);
  207. procedure AddExternalImport(const libname,symname,symmangledname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  208. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  209. end;
  210. tused_unit = class(tlinkedlistitem)
  211. checksum,
  212. interface_checksum,
  213. indirect_checksum: cardinal;
  214. in_uses,
  215. in_interface : boolean;
  216. u : tmodule;
  217. unitsym : tunitsym;
  218. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  219. procedure check_hints;
  220. end;
  221. tdependent_unit = class(tlinkedlistitem)
  222. u : tmodule;
  223. constructor create(_u : tmodule);
  224. end;
  225. var
  226. main_module : tmodule; { Main module of the program }
  227. current_module : tmodule; { Current module which is compiled or loaded }
  228. compiled_module : tmodule; { Current module which is compiled }
  229. usedunits : tlinkedlist; { Used units for this program }
  230. loaded_units : tlinkedlist; { All loaded units }
  231. unloaded_units : tlinkedlist; { Units removed from loaded_units, to be freed }
  232. SmartLinkOFiles : TCmdStrList; { List of .o files which are generated,
  233. used to delete them after linking }
  234. procedure set_current_module(p:tmodule);
  235. function get_module(moduleindex : longint) : tmodule;
  236. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  237. procedure addloadedunit(hp:tmodule);
  238. function find_module_from_symtable(st:tsymtable):tmodule;
  239. implementation
  240. uses
  241. SysUtils,globals,
  242. verbose,systems,
  243. scanner,ppu,dbgbase,
  244. procinfo,symdef;
  245. {$ifdef MEMDEBUG}
  246. var
  247. memsymtable : TMemDebug;
  248. {$endif}
  249. {*****************************************************************************
  250. Global Functions
  251. *****************************************************************************}
  252. function find_module_from_symtable(st:tsymtable):tmodule;
  253. var
  254. hp : tmodule;
  255. begin
  256. result:=nil;
  257. hp:=tmodule(loaded_units.first);
  258. while assigned(hp) do
  259. begin
  260. if (hp.moduleid=st.moduleid) then
  261. begin
  262. result:=hp;
  263. exit;
  264. end;
  265. hp:=tmodule(hp.next);
  266. end;
  267. end;
  268. procedure set_current_module(p:tmodule);
  269. begin
  270. { save the state of the scanner }
  271. if assigned(current_scanner) then
  272. current_scanner.tempcloseinputfile;
  273. { set new module }
  274. current_module:=p;
  275. { restore previous module settings }
  276. Fillchar(current_filepos,0,sizeof(current_filepos));
  277. if assigned(current_module) then
  278. begin
  279. current_asmdata:=tasmdata(current_module.asmdata);
  280. current_debuginfo:=tdebuginfo(current_module.debuginfo);
  281. { restore scanner and file positions }
  282. current_scanner:=tscannerfile(current_module.scanner);
  283. if assigned(current_scanner) then
  284. begin
  285. current_scanner.tempopeninputfile;
  286. current_scanner.gettokenpos;
  287. parser_current_file:=current_scanner.inputfile.name;
  288. end
  289. else
  290. begin
  291. current_filepos.moduleindex:=current_module.unit_index;
  292. parser_current_file:='';
  293. end;
  294. end
  295. else
  296. begin
  297. current_asmdata:=nil;
  298. current_scanner:=nil;
  299. current_debuginfo:=nil;
  300. end;
  301. end;
  302. function get_module(moduleindex : longint) : tmodule;
  303. var
  304. hp : tmodule;
  305. begin
  306. result:=nil;
  307. if moduleindex=0 then
  308. exit;
  309. result:=current_module;
  310. if not(assigned(loaded_units)) then
  311. exit;
  312. hp:=tmodule(loaded_units.first);
  313. while assigned(hp) and (hp.unit_index<>moduleindex) do
  314. hp:=tmodule(hp.next);
  315. result:=hp;
  316. end;
  317. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  318. var
  319. hp : tmodule;
  320. begin
  321. hp:=get_module(moduleindex);
  322. if assigned(hp) then
  323. get_source_file:=hp.sourcefiles.get_file(fileindex)
  324. else
  325. get_source_file:=nil;
  326. end;
  327. procedure addloadedunit(hp:tmodule);
  328. begin
  329. hp.moduleid:=loaded_units.count;
  330. loaded_units.concat(hp);
  331. end;
  332. {****************************************************************************
  333. TLinkContainerItem
  334. ****************************************************************************}
  335. constructor TLinkContainerItem.Create(const s:TPathStr;m:cardinal);
  336. begin
  337. inherited Create;
  338. data:=s;
  339. needlink:=m;
  340. end;
  341. {****************************************************************************
  342. TLinkContainer
  343. ****************************************************************************}
  344. procedure TLinkContainer.add(const s : TPathStr;m:cardinal);
  345. begin
  346. inherited concat(TLinkContainerItem.Create(s,m));
  347. end;
  348. function TLinkContainer.get(var m:cardinal) : TPathStr;
  349. var
  350. p : tlinkcontaineritem;
  351. begin
  352. p:=tlinkcontaineritem(inherited getfirst);
  353. if p=nil then
  354. begin
  355. get:='';
  356. m:=0;
  357. end
  358. else
  359. begin
  360. get:=p.data;
  361. m:=p.needlink;
  362. p.free;
  363. end;
  364. end;
  365. function TLinkContainer.getusemask(mask:cardinal) : TPathStr;
  366. var
  367. p : tlinkcontaineritem;
  368. found : boolean;
  369. begin
  370. found:=false;
  371. repeat
  372. p:=tlinkcontaineritem(inherited getfirst);
  373. if p=nil then
  374. begin
  375. getusemask:='';
  376. exit;
  377. end;
  378. getusemask:=p.data;
  379. found:=(p.needlink and mask)<>0;
  380. p.free;
  381. until found;
  382. end;
  383. function TLinkContainer.find(const s:TPathStr):boolean;
  384. var
  385. newnode : tlinkcontaineritem;
  386. begin
  387. find:=false;
  388. newnode:=tlinkcontaineritem(First);
  389. while assigned(newnode) do
  390. begin
  391. if newnode.data=s then
  392. begin
  393. find:=true;
  394. exit;
  395. end;
  396. newnode:=tlinkcontaineritem(newnode.next);
  397. end;
  398. end;
  399. {****************************************************************************
  400. TUSED_UNIT
  401. ****************************************************************************}
  402. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  403. begin
  404. u:=_u;
  405. in_interface:=intface;
  406. in_uses:=inuses;
  407. unitsym:=usym;
  408. if _u.state=ms_compiled then
  409. begin
  410. checksum:=u.crc;
  411. interface_checksum:=u.interface_crc;
  412. indirect_checksum:=u.indirect_crc;
  413. end
  414. else
  415. begin
  416. checksum:=0;
  417. interface_checksum:=0;
  418. indirect_checksum:=0;
  419. end;
  420. end;
  421. procedure tused_unit.check_hints;
  422. var
  423. uname: pshortstring;
  424. begin
  425. uname:=u.realmodulename;
  426. if mo_hint_deprecated in u.moduleoptions then
  427. if (mo_has_deprecated_msg in u.moduleoptions) and (u.deprecatedmsg <> nil) then
  428. MessagePos2(unitsym.fileinfo,sym_w_deprecated_unit_with_msg,uname^,u.deprecatedmsg^)
  429. else
  430. MessagePos1(unitsym.fileinfo,sym_w_deprecated_unit,uname^);
  431. if mo_hint_experimental in u.moduleoptions then
  432. MessagePos1(unitsym.fileinfo,sym_w_experimental_unit,uname^);
  433. if mo_hint_platform in u.moduleoptions then
  434. MessagePos1(unitsym.fileinfo,sym_w_non_portable_unit,uname^);
  435. if mo_hint_library in u.moduleoptions then
  436. MessagePos1(unitsym.fileinfo,sym_w_library_unit,uname^);
  437. if mo_hint_unimplemented in u.moduleoptions then
  438. MessagePos1(unitsym.fileinfo,sym_w_non_implemented_unit,uname^);
  439. end;
  440. {****************************************************************************
  441. TDENPENDENT_UNIT
  442. ****************************************************************************}
  443. constructor tdependent_unit.create(_u : tmodule);
  444. begin
  445. u:=_u;
  446. end;
  447. {****************************************************************************
  448. TMODULE
  449. ****************************************************************************}
  450. constructor tmodule.create(LoadedFrom:TModule;const amodulename: string; const afilename:TPathStr;_is_unit:boolean);
  451. var
  452. n:string;
  453. fn:TPathStr;
  454. begin
  455. if amodulename='' then
  456. n:=ChangeFileExt(ExtractFileName(afilename),'')
  457. else
  458. n:=amodulename;
  459. if afilename='' then
  460. fn:=amodulename
  461. else
  462. fn:=afilename;
  463. { Programs have the name 'Program' to don't conflict with dup id's }
  464. if _is_unit then
  465. inherited create(amodulename)
  466. else
  467. inherited create('Program');
  468. mainsource:=fn;
  469. { Dos has the famous 8.3 limit :( }
  470. {$ifdef shortasmprefix}
  471. asmprefix:=stringdup(FixFileName('as'));
  472. {$else}
  473. asmprefix:=stringdup(FixFileName(n));
  474. {$endif}
  475. setfilename(fn,true);
  476. localunitsearchpath:=TSearchPathList.Create;
  477. localobjectsearchpath:=TSearchPathList.Create;
  478. localincludesearchpath:=TSearchPathList.Create;
  479. locallibrarysearchpath:=TSearchPathList.Create;
  480. localframeworksearchpath:=TSearchPathList.Create;
  481. used_units:=TLinkedList.Create;
  482. dependent_units:=TLinkedList.Create;
  483. resourcefiles:=TCmdStrList.Create;
  484. linkunitofiles:=TLinkContainer.Create;
  485. linkunitstaticlibs:=TLinkContainer.Create;
  486. linkunitsharedlibs:=TLinkContainer.Create;
  487. linkotherofiles:=TLinkContainer.Create;
  488. linkotherstaticlibs:=TLinkContainer.Create;
  489. linkothersharedlibs:=TLinkContainer.Create;
  490. linkotherframeworks:=TLinkContainer.Create;
  491. mainname:=nil;
  492. FImportLibraryList:=TFPHashObjectList.Create(true);
  493. crc:=0;
  494. interface_crc:=0;
  495. indirect_crc:=0;
  496. flags:=0;
  497. scanner:=nil;
  498. unitmap:=nil;
  499. unitmapsize:=0;
  500. derefmap:=nil;
  501. derefmapsize:=0;
  502. derefmapcnt:=0;
  503. derefdata:=TDynamicArray.Create(1024);
  504. derefdataintflen:=0;
  505. deflist:=TFPObjectList.Create(false);
  506. symlist:=TFPObjectList.Create(false);
  507. ptrdefs:=THashSet.Create(64,true,false);
  508. arraydefs:=THashSet.Create(64,true,false);
  509. procaddrdefs:=THashSet.Create(64,true,false);
  510. {$ifdef llvm}
  511. llvmdefs:=THashSet.Create(64,true,false);
  512. {$endif llvm}
  513. ansistrdef:=nil;
  514. wpoinfo:=nil;
  515. checkforwarddefs:=TFPObjectList.Create(false);
  516. extendeddefs:=TFPHashObjectList.Create(true);
  517. genericdummysyms:=tfphashobjectlist.create(true);
  518. waitingforunit:=tfpobjectlist.create(false);
  519. waitingunits:=tfpobjectlist.create(false);
  520. globalsymtable:=nil;
  521. localsymtable:=nil;
  522. globalmacrosymtable:=nil;
  523. localmacrosymtable:=nil;
  524. loaded_from:=LoadedFrom;
  525. do_reload:=false;
  526. do_compile:=false;
  527. sources_avail:=true;
  528. mainfilepos.line:=0;
  529. mainfilepos.column:=0;
  530. mainfilepos.fileindex:=0;
  531. recompile_reason:=rr_unknown;
  532. in_interface:=true;
  533. in_global:=true;
  534. is_unit:=_is_unit;
  535. islibrary:=false;
  536. ispackage:=false;
  537. is_dbginfo_written:=false;
  538. mode_switch_allowed:= true;
  539. moduleoptions:=[];
  540. deprecatedmsg:=nil;
  541. namespace:=nil;
  542. tcinitcode:=nil;
  543. _exports:=TLinkedList.Create;
  544. dllscannerinputlist:=TFPHashList.Create;
  545. asmdata:=casmdata.create(modulename);
  546. unitimportsyms:=TFPObjectList.Create(false);
  547. InitDebugInfo(self,false);
  548. end;
  549. destructor tmodule.Destroy;
  550. var
  551. i : longint;
  552. current_debuginfo_reset : boolean;
  553. begin
  554. if assigned(unitmap) then
  555. freemem(unitmap);
  556. if assigned(derefmap) then
  557. begin
  558. for i:=0 to derefmapcnt-1 do
  559. stringdispose(derefmap[i].modulename);
  560. freemem(derefmap);
  561. end;
  562. if assigned(_exports) then
  563. _exports.free;
  564. if assigned(dllscannerinputlist) then
  565. dllscannerinputlist.free;
  566. if assigned(scanner) then
  567. begin
  568. { also update current_scanner if it was pointing
  569. to this module }
  570. if current_scanner=tscannerfile(scanner) then
  571. current_scanner:=nil;
  572. tscannerfile(scanner).free;
  573. end;
  574. if assigned(asmdata) then
  575. begin
  576. if current_asmdata=asmdata then
  577. current_asmdata:=nil;
  578. asmdata.free;
  579. end;
  580. if assigned(procinfo) then
  581. begin
  582. if current_procinfo=tprocinfo(procinfo) then
  583. begin
  584. current_procinfo:=nil;
  585. current_structdef:=nil;
  586. current_genericdef:=nil;
  587. current_specializedef:=nil;
  588. end;
  589. { release procinfo tree }
  590. tprocinfo(procinfo).destroy_tree;
  591. end;
  592. DoneDebugInfo(self,current_debuginfo_reset);
  593. used_units.free;
  594. dependent_units.free;
  595. resourcefiles.Free;
  596. linkunitofiles.Free;
  597. linkunitstaticlibs.Free;
  598. linkunitsharedlibs.Free;
  599. linkotherofiles.Free;
  600. linkotherstaticlibs.Free;
  601. linkothersharedlibs.Free;
  602. linkotherframeworks.Free;
  603. stringdispose(mainname);
  604. unitimportsyms.Free;
  605. FImportLibraryList.Free;
  606. extendeddefs.Free;
  607. genericdummysyms.free;
  608. waitingforunit.free;
  609. waitingunits.free;
  610. stringdispose(asmprefix);
  611. stringdispose(deprecatedmsg);
  612. stringdispose(namespace);
  613. tcinitcode.free;
  614. localunitsearchpath.Free;
  615. localobjectsearchpath.free;
  616. localincludesearchpath.free;
  617. locallibrarysearchpath.free;
  618. localframeworksearchpath.free;
  619. {$ifdef MEMDEBUG}
  620. memsymtable.start;
  621. {$endif}
  622. derefdata.free;
  623. deflist.free;
  624. symlist.free;
  625. ptrdefs.free;
  626. arraydefs.free;
  627. procaddrdefs.free;
  628. {$ifdef llvm}
  629. llvmdefs.free;
  630. {$endif llvm}
  631. ansistrdef:=nil;
  632. wpoinfo.free;
  633. checkforwarddefs.free;
  634. globalsymtable.free;
  635. localsymtable.free;
  636. globalmacrosymtable.free;
  637. localmacrosymtable.free;
  638. {$ifdef MEMDEBUG}
  639. memsymtable.stop;
  640. {$endif}
  641. inherited Destroy;
  642. end;
  643. procedure tmodule.reset;
  644. var
  645. i : longint;
  646. current_debuginfo_reset : boolean;
  647. begin
  648. if assigned(scanner) then
  649. begin
  650. { also update current_scanner if it was pointing
  651. to this module }
  652. if current_scanner=tscannerfile(scanner) then
  653. current_scanner:=nil;
  654. tscannerfile(scanner).free;
  655. scanner:=nil;
  656. end;
  657. if assigned(procinfo) then
  658. begin
  659. if current_procinfo=tprocinfo(procinfo) then
  660. begin
  661. current_procinfo:=nil;
  662. current_structdef:=nil;
  663. current_genericdef:=nil;
  664. current_specializedef:=nil;
  665. end;
  666. { release procinfo tree }
  667. tprocinfo(procinfo).destroy_tree;
  668. end;
  669. if assigned(asmdata) then
  670. begin
  671. if current_asmdata=asmdata then
  672. current_asmdata:=nil;
  673. asmdata.free;
  674. asmdata:=nil;
  675. end;
  676. DoneDebugInfo(self,current_debuginfo_reset);
  677. globalsymtable.free;
  678. globalsymtable:=nil;
  679. localsymtable.free;
  680. localsymtable:=nil;
  681. globalmacrosymtable.free;
  682. globalmacrosymtable:=nil;
  683. localmacrosymtable.free;
  684. localmacrosymtable:=nil;
  685. deflist.free;
  686. deflist:=TFPObjectList.Create(false);
  687. symlist.free;
  688. symlist:=TFPObjectList.Create(false);
  689. ptrdefs.free;
  690. ptrdefs:=THashSet.Create(64,true,false);
  691. arraydefs.free;
  692. arraydefs:=THashSet.Create(64,true,false);
  693. procaddrdefs.free;
  694. procaddrdefs:=THashSet.Create(64,true,false);
  695. {$ifdef llvm}
  696. llvmdefs.free;
  697. llvmdefs:=THashSet.Create(64,true,false);
  698. {$endif llvm}
  699. wpoinfo.free;
  700. wpoinfo:=nil;
  701. checkforwarddefs.free;
  702. checkforwarddefs:=TFPObjectList.Create(false);
  703. unitimportsyms.free;
  704. unitimportsyms:=TFPObjectList.Create(false);
  705. derefdata.free;
  706. derefdata:=TDynamicArray.Create(1024);
  707. if assigned(unitmap) then
  708. begin
  709. freemem(unitmap);
  710. unitmap:=nil;
  711. end;
  712. if assigned(derefmap) then
  713. begin
  714. for i:=0 to derefmapcnt-1 do
  715. stringdispose(derefmap[i].modulename);
  716. freemem(derefmap);
  717. derefmap:=nil;
  718. end;
  719. unitmapsize:=0;
  720. derefmapsize:=0;
  721. derefmapcnt:=0;
  722. derefdataintflen:=0;
  723. sourcefiles.free;
  724. sourcefiles:=tinputfilemanager.create;
  725. asmdata:=casmdata.create(modulename);
  726. InitDebugInfo(self,current_debuginfo_reset);
  727. _exports.free;
  728. _exports:=tlinkedlist.create;
  729. dllscannerinputlist.free;
  730. dllscannerinputlist:=TFPHashList.create;
  731. used_units.free;
  732. used_units:=TLinkedList.Create;
  733. dependent_units.free;
  734. dependent_units:=TLinkedList.Create;
  735. resourcefiles.Free;
  736. resourcefiles:=TCmdStrList.Create;
  737. linkunitofiles.Free;
  738. linkunitofiles:=TLinkContainer.Create;
  739. linkunitstaticlibs.Free;
  740. linkunitstaticlibs:=TLinkContainer.Create;
  741. linkunitsharedlibs.Free;
  742. linkunitsharedlibs:=TLinkContainer.Create;
  743. linkotherofiles.Free;
  744. linkotherofiles:=TLinkContainer.Create;
  745. linkotherstaticlibs.Free;
  746. linkotherstaticlibs:=TLinkContainer.Create;
  747. linkothersharedlibs.Free;
  748. linkothersharedlibs:=TLinkContainer.Create;
  749. linkotherframeworks.Free;
  750. linkotherframeworks:=TLinkContainer.Create;
  751. stringdispose(mainname);
  752. FImportLibraryList.Free;
  753. FImportLibraryList:=TFPHashObjectList.Create;
  754. do_compile:=false;
  755. do_reload:=false;
  756. interface_compiled:=false;
  757. in_interface:=true;
  758. in_global:=true;
  759. mode_switch_allowed:=true;
  760. stringdispose(deprecatedmsg);
  761. stringdispose(namespace);
  762. tcinitcode.free;
  763. tcinitcode:=nil;
  764. moduleoptions:=[];
  765. is_dbginfo_written:=false;
  766. crc:=0;
  767. interface_crc:=0;
  768. indirect_crc:=0;
  769. flags:=0;
  770. mainfilepos.line:=0;
  771. mainfilepos.column:=0;
  772. mainfilepos.fileindex:=0;
  773. recompile_reason:=rr_unknown;
  774. {
  775. The following fields should not
  776. be reset:
  777. mainsource
  778. state
  779. loaded_from
  780. sources_avail
  781. }
  782. end;
  783. procedure tmodule.adddependency(callermodule:tmodule);
  784. begin
  785. { This is not needed for programs }
  786. if not callermodule.is_unit then
  787. exit;
  788. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  789. dependent_units.concat(tdependent_unit.create(callermodule));
  790. end;
  791. procedure tmodule.flagdependent(callermodule:tmodule);
  792. var
  793. pm : tdependent_unit;
  794. begin
  795. { flag all units that depend on this unit for reloading }
  796. pm:=tdependent_unit(current_module.dependent_units.first);
  797. while assigned(pm) do
  798. begin
  799. { We do not have to reload the unit that wants to load
  800. this unit, unless this unit is already compiled during
  801. the loading }
  802. if (pm.u=callermodule) and
  803. (pm.u.state<>ms_compiled) then
  804. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  805. else
  806. if pm.u.state=ms_second_compile then
  807. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  808. else
  809. begin
  810. pm.u.do_reload:=true;
  811. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  812. end;
  813. pm:=tdependent_unit(pm.next);
  814. end;
  815. end;
  816. procedure tmodule.addimportedsym(sym:TSymEntry);
  817. begin
  818. if unitimportsyms.IndexOf(sym)<0 then
  819. unitimportsyms.Add(sym);
  820. end;
  821. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  822. var
  823. pu : tused_unit;
  824. begin
  825. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  826. used_units.concat(pu);
  827. addusedunit:=pu;
  828. end;
  829. procedure tmodule.updatemaps;
  830. var
  831. oldmapsize : longint;
  832. hp : tmodule;
  833. i : longint;
  834. begin
  835. { Extend unitmap }
  836. oldmapsize:=unitmapsize;
  837. unitmapsize:=loaded_units.count;
  838. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  839. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  840. { Extend Derefmap }
  841. oldmapsize:=derefmapsize;
  842. derefmapsize:=loaded_units.count;
  843. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  844. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  845. { Add all units to unitmap }
  846. hp:=tmodule(loaded_units.first);
  847. i:=0;
  848. while assigned(hp) do
  849. begin
  850. if hp.moduleid>=unitmapsize then
  851. internalerror(200501151);
  852. { Verify old entries }
  853. if (i<oldmapsize) then
  854. begin
  855. if (hp.moduleid<>i) or
  856. (unitmap[hp.moduleid].u<>hp) then
  857. internalerror(200501156);
  858. end
  859. else
  860. begin
  861. unitmap[hp.moduleid].u:=hp;
  862. unitmap[hp.moduleid].derefidx:=-1;
  863. end;
  864. inc(i);
  865. hp:=tmodule(hp.next);
  866. end;
  867. end;
  868. function tmodule.derefidx_unit(id:longint):longint;
  869. begin
  870. if id>=unitmapsize then
  871. internalerror(2005011511);
  872. if unitmap[id].derefidx=-1 then
  873. begin
  874. unitmap[id].derefidx:=derefmapcnt;
  875. inc(derefmapcnt);
  876. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  877. end;
  878. if unitmap[id].derefidx>=derefmapsize then
  879. internalerror(2005011514);
  880. result:=unitmap[id].derefidx;
  881. end;
  882. function tmodule.resolve_unit(id:longint):tmodule;
  883. var
  884. hp : tmodule;
  885. begin
  886. if id>=derefmapsize then
  887. internalerror(200306231);
  888. result:=derefmap[id].u;
  889. if not assigned(result) then
  890. begin
  891. if not assigned(derefmap[id].modulename) or
  892. (derefmap[id].modulename^='') then
  893. internalerror(200501159);
  894. hp:=tmodule(loaded_units.first);
  895. while assigned(hp) do
  896. begin
  897. { only check for units. The main program is also
  898. as a unit in the loaded_units list. We simply need
  899. to ignore this entry (PFV) }
  900. if hp.is_unit and
  901. (hp.modulename^=derefmap[id].modulename^) then
  902. break;
  903. hp:=tmodule(hp.next);
  904. end;
  905. if not assigned(hp) then
  906. internalerror(2005011510);
  907. derefmap[id].u:=hp;
  908. result:=hp;
  909. end;
  910. end;
  911. procedure tmodule.allunitsused;
  912. var
  913. pu : tused_unit;
  914. begin
  915. pu:=tused_unit(used_units.first);
  916. while assigned(pu) do
  917. begin
  918. if assigned(pu.u.globalsymtable) then
  919. begin
  920. if unitmap[pu.u.moduleid].u<>pu.u then
  921. internalerror(200501157);
  922. { Give a note when the unit is not referenced, skip
  923. this is for units with an initialization/finalization }
  924. if (unitmap[pu.u.moduleid].refs=0) and
  925. pu.in_uses and
  926. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  927. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  928. end;
  929. pu:=tused_unit(pu.next);
  930. end;
  931. end;
  932. procedure tmodule.end_of_parsing;
  933. begin
  934. { free asmdata }
  935. if assigned(asmdata) then
  936. begin
  937. asmdata.free;
  938. asmdata:=nil;
  939. end;
  940. { free scanner }
  941. if assigned(scanner) then
  942. begin
  943. if current_scanner=tscannerfile(scanner) then
  944. current_scanner:=nil;
  945. tscannerfile(scanner).free;
  946. scanner:=nil;
  947. end;
  948. { free symtable stack }
  949. if assigned(symtablestack) then
  950. begin
  951. symtablestack.free;
  952. symtablestack:=nil;
  953. end;
  954. if assigned(macrosymtablestack) then
  955. begin
  956. macrosymtablestack.free;
  957. macrosymtablestack:=nil;
  958. end;
  959. extendeddefs.free;
  960. extendeddefs:=nil;
  961. genericdummysyms.free;
  962. genericdummysyms:=nil;
  963. waitingforunit.free;
  964. waitingforunit:=nil;
  965. localmacrosymtable.free;
  966. localmacrosymtable:=nil;
  967. ptrdefs.free;
  968. ptrdefs:=nil;
  969. arraydefs.free;
  970. arraydefs:=nil;
  971. procaddrdefs.free;
  972. procaddrdefs:=nil;
  973. {$ifdef llvm}
  974. llvmdefs.free;
  975. llvmdefs:=nil;
  976. {$endif llvm}
  977. checkforwarddefs.free;
  978. checkforwarddefs:=nil;
  979. tcinitcode.free;
  980. tcinitcode:=nil;
  981. localunitsearchpath.free;
  982. localunitsearchpath:=nil;
  983. localobjectsearchpath.free;
  984. localobjectsearchpath:=nil;
  985. localincludesearchpath.free;
  986. localincludesearchpath:=nil;
  987. locallibrarysearchpath.free;
  988. locallibrarysearchpath:=nil;
  989. localframeworksearchpath.free;
  990. localframeworksearchpath:=nil;
  991. end;
  992. procedure tmodule.setmodulename(const s:string);
  993. begin
  994. stringdispose(modulename);
  995. stringdispose(realmodulename);
  996. modulename:=stringdup(upper(s));
  997. realmodulename:=stringdup(s);
  998. { also update asmlibrary names }
  999. current_asmdata.name:=modulename;
  1000. end;
  1001. procedure TModule.AddExternalImport(const libname,symname,symmangledname:string;
  1002. OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  1003. var
  1004. ImportLibrary,OtherIL : TImportLibrary;
  1005. ImportSymbol : TImportSymbol;
  1006. i : longint;
  1007. begin
  1008. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  1009. if not assigned(ImportLibrary) then
  1010. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  1011. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  1012. if not assigned(ImportSymbol) then
  1013. begin
  1014. { Check that the same name does not exist in another library }
  1015. { If it does and the same mangled name is used, issue a warning }
  1016. if ImportLibraryList.Count>1 then
  1017. for i:=0 To ImportLibraryList.Count-1 do
  1018. begin
  1019. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  1020. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  1021. if assigned(ImportSymbol) then
  1022. begin
  1023. if ImportSymbol.MangledName=symmangledname then
  1024. begin
  1025. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  1026. break;
  1027. end;
  1028. end;
  1029. end;
  1030. if not ImportByOrdinalOnly then
  1031. { negative ordinal number indicates import by name with ordinal number as hint }
  1032. OrdNr:=-OrdNr;
  1033. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  1034. symname,symmangledname,OrdNr,isvar);
  1035. end;
  1036. end;
  1037. initialization
  1038. {$ifdef MEMDEBUG}
  1039. memsymtable:=TMemDebug.create('Symtables');
  1040. memsymtable.stop;
  1041. {$endif MEMDEBUG}
  1042. finalization
  1043. {$ifdef MEMDEBUG}
  1044. memsymtable.free;
  1045. {$endif MEMDEBUG}
  1046. end.