fmodule.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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. linkunitofiles.Free;
  745. linkunitofiles:=TLinkContainer.Create;
  746. linkunitstaticlibs.Free;
  747. linkunitstaticlibs:=TLinkContainer.Create;
  748. linkunitsharedlibs.Free;
  749. linkunitsharedlibs:=TLinkContainer.Create;
  750. linkotherofiles.Free;
  751. linkotherofiles:=TLinkContainer.Create;
  752. linkotherstaticlibs.Free;
  753. linkotherstaticlibs:=TLinkContainer.Create;
  754. linkothersharedlibs.Free;
  755. linkothersharedlibs:=TLinkContainer.Create;
  756. linkotherframeworks.Free;
  757. linkotherframeworks:=TLinkContainer.Create;
  758. stringdispose(mainname);
  759. FImportLibraryList.Free;
  760. FImportLibraryList:=TFPHashObjectList.Create;
  761. do_compile:=false;
  762. do_reload:=false;
  763. interface_compiled:=false;
  764. in_interface:=true;
  765. in_global:=true;
  766. mode_switch_allowed:=true;
  767. stringdispose(deprecatedmsg);
  768. stringdispose(namespace);
  769. tcinitcode.free;
  770. tcinitcode:=nil;
  771. moduleoptions:=[];
  772. is_dbginfo_written:=false;
  773. crc:=0;
  774. interface_crc:=0;
  775. indirect_crc:=0;
  776. flags:=0;
  777. mainfilepos.line:=0;
  778. mainfilepos.column:=0;
  779. mainfilepos.fileindex:=0;
  780. recompile_reason:=rr_unknown;
  781. {
  782. The following fields should not
  783. be reset:
  784. mainsource
  785. state
  786. loaded_from
  787. sources_avail
  788. }
  789. end;
  790. procedure tmodule.adddependency(callermodule:tmodule);
  791. begin
  792. { This is not needed for programs }
  793. if not callermodule.is_unit then
  794. exit;
  795. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  796. dependent_units.concat(tdependent_unit.create(callermodule));
  797. end;
  798. procedure tmodule.flagdependent(callermodule:tmodule);
  799. var
  800. pm : tdependent_unit;
  801. begin
  802. { flag all units that depend on this unit for reloading }
  803. pm:=tdependent_unit(current_module.dependent_units.first);
  804. while assigned(pm) do
  805. begin
  806. { We do not have to reload the unit that wants to load
  807. this unit, unless this unit is already compiled during
  808. the loading }
  809. if (pm.u=callermodule) and
  810. (pm.u.state<>ms_compiled) then
  811. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  812. else
  813. if pm.u.state=ms_second_compile then
  814. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  815. else
  816. begin
  817. pm.u.do_reload:=true;
  818. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  819. end;
  820. pm:=tdependent_unit(pm.next);
  821. end;
  822. end;
  823. procedure tmodule.addimportedsym(sym:TSymEntry);
  824. begin
  825. if unitimportsyms.IndexOf(sym)<0 then
  826. unitimportsyms.Add(sym);
  827. end;
  828. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  829. var
  830. pu : tused_unit;
  831. begin
  832. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  833. used_units.concat(pu);
  834. addusedunit:=pu;
  835. end;
  836. procedure tmodule.updatemaps;
  837. var
  838. oldmapsize : longint;
  839. hp : tmodule;
  840. i : longint;
  841. begin
  842. { Extend unitmap }
  843. oldmapsize:=unitmapsize;
  844. unitmapsize:=loaded_units.count;
  845. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  846. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  847. { Extend Derefmap }
  848. oldmapsize:=derefmapsize;
  849. derefmapsize:=loaded_units.count;
  850. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  851. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  852. { Add all units to unitmap }
  853. hp:=tmodule(loaded_units.first);
  854. i:=0;
  855. while assigned(hp) do
  856. begin
  857. if hp.moduleid>=unitmapsize then
  858. internalerror(200501151);
  859. { Verify old entries }
  860. if (i<oldmapsize) then
  861. begin
  862. if (hp.moduleid<>i) or
  863. (unitmap[hp.moduleid].u<>hp) then
  864. internalerror(200501156);
  865. end
  866. else
  867. begin
  868. unitmap[hp.moduleid].u:=hp;
  869. unitmap[hp.moduleid].derefidx:=-1;
  870. end;
  871. inc(i);
  872. hp:=tmodule(hp.next);
  873. end;
  874. end;
  875. function tmodule.derefidx_unit(id:longint):longint;
  876. begin
  877. if id>=unitmapsize then
  878. internalerror(2005011511);
  879. if unitmap[id].derefidx=-1 then
  880. begin
  881. unitmap[id].derefidx:=derefmapcnt;
  882. inc(derefmapcnt);
  883. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  884. end;
  885. if unitmap[id].derefidx>=derefmapsize then
  886. internalerror(2005011514);
  887. result:=unitmap[id].derefidx;
  888. end;
  889. function tmodule.resolve_unit(id:longint):tmodule;
  890. var
  891. hp : tmodule;
  892. begin
  893. if id>=derefmapsize then
  894. internalerror(200306231);
  895. result:=derefmap[id].u;
  896. if not assigned(result) then
  897. begin
  898. if not assigned(derefmap[id].modulename) or
  899. (derefmap[id].modulename^='') then
  900. internalerror(200501159);
  901. hp:=tmodule(loaded_units.first);
  902. while assigned(hp) do
  903. begin
  904. { only check for units. The main program is also
  905. as a unit in the loaded_units list. We simply need
  906. to ignore this entry (PFV) }
  907. if hp.is_unit and
  908. (hp.modulename^=derefmap[id].modulename^) then
  909. break;
  910. hp:=tmodule(hp.next);
  911. end;
  912. if not assigned(hp) then
  913. internalerror(2005011510);
  914. derefmap[id].u:=hp;
  915. result:=hp;
  916. end;
  917. end;
  918. procedure tmodule.allunitsused;
  919. var
  920. pu : tused_unit;
  921. begin
  922. pu:=tused_unit(used_units.first);
  923. while assigned(pu) do
  924. begin
  925. if assigned(pu.u.globalsymtable) then
  926. begin
  927. if unitmap[pu.u.moduleid].u<>pu.u then
  928. internalerror(200501157);
  929. { Give a note when the unit is not referenced, skip
  930. this is for units with an initialization/finalization }
  931. if (unitmap[pu.u.moduleid].refs=0) and
  932. pu.in_uses and
  933. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  934. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  935. end;
  936. pu:=tused_unit(pu.next);
  937. end;
  938. end;
  939. procedure tmodule.end_of_parsing;
  940. begin
  941. { free asmdata }
  942. if assigned(asmdata) then
  943. begin
  944. asmdata.free;
  945. asmdata:=nil;
  946. end;
  947. { free scanner }
  948. if assigned(scanner) then
  949. begin
  950. if current_scanner=tscannerfile(scanner) then
  951. current_scanner:=nil;
  952. tscannerfile(scanner).free;
  953. scanner:=nil;
  954. end;
  955. { free symtable stack }
  956. if assigned(symtablestack) then
  957. begin
  958. symtablestack.free;
  959. symtablestack:=nil;
  960. end;
  961. if assigned(macrosymtablestack) then
  962. begin
  963. macrosymtablestack.free;
  964. macrosymtablestack:=nil;
  965. end;
  966. waitingforunit.free;
  967. waitingforunit:=nil;
  968. localmacrosymtable.free;
  969. localmacrosymtable:=nil;
  970. ptrdefs.free;
  971. ptrdefs:=nil;
  972. arraydefs.free;
  973. arraydefs:=nil;
  974. procaddrdefs.free;
  975. procaddrdefs:=nil;
  976. {$ifdef llvm}
  977. llvmdefs.free;
  978. llvmdefs:=nil;
  979. {$endif llvm}
  980. checkforwarddefs.free;
  981. checkforwarddefs:=nil;
  982. tcinitcode.free;
  983. tcinitcode:=nil;
  984. localunitsearchpath.free;
  985. localunitsearchpath:=nil;
  986. localobjectsearchpath.free;
  987. localobjectsearchpath:=nil;
  988. localincludesearchpath.free;
  989. localincludesearchpath:=nil;
  990. locallibrarysearchpath.free;
  991. locallibrarysearchpath:=nil;
  992. localframeworksearchpath.free;
  993. localframeworksearchpath:=nil;
  994. end;
  995. procedure tmodule.setmodulename(const s:string);
  996. begin
  997. stringdispose(modulename);
  998. stringdispose(realmodulename);
  999. modulename:=stringdup(upper(s));
  1000. realmodulename:=stringdup(s);
  1001. { also update asmlibrary names }
  1002. current_asmdata.name:=modulename;
  1003. end;
  1004. procedure TModule.AddExternalImport(const libname,symname,symmangledname:string;
  1005. OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  1006. var
  1007. ImportLibrary,OtherIL : TImportLibrary;
  1008. ImportSymbol : TImportSymbol;
  1009. i : longint;
  1010. begin
  1011. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  1012. if not assigned(ImportLibrary) then
  1013. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  1014. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  1015. if not assigned(ImportSymbol) then
  1016. begin
  1017. { Check that the same name does not exist in another library }
  1018. { If it does and the same mangled name is used, issue a warning }
  1019. if ImportLibraryList.Count>1 then
  1020. for i:=0 To ImportLibraryList.Count-1 do
  1021. begin
  1022. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  1023. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  1024. if assigned(ImportSymbol) then
  1025. begin
  1026. if ImportSymbol.MangledName=symmangledname then
  1027. begin
  1028. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  1029. break;
  1030. end;
  1031. end;
  1032. end;
  1033. if not ImportByOrdinalOnly then
  1034. { negative ordinal number indicates import by name with ordinal number as hint }
  1035. OrdNr:=-OrdNr;
  1036. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  1037. symname,symmangledname,OrdNr,isvar);
  1038. end;
  1039. end;
  1040. initialization
  1041. {$ifdef MEMDEBUG}
  1042. memsymtable:=TMemDebug.create('Symtables');
  1043. memsymtable.stop;
  1044. {$endif MEMDEBUG}
  1045. finalization
  1046. {$ifdef MEMDEBUG}
  1047. memsymtable.free;
  1048. {$endif MEMDEBUG}
  1049. end.