fmodule.pas 37 KB

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