fmodule.pas 35 KB

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