fmodule.pas 39 KB

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