fmodule.pas 36 KB

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