fmodule.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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 atari}
  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,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. {$ifdef VER3_2}
  48. RTLString = ansistring;
  49. {$endif VER3_2}
  50. { unit options }
  51. tmoduleoption = (mo_none,
  52. mo_hint_deprecated,
  53. mo_hint_platform,
  54. mo_hint_library,
  55. mo_hint_unimplemented,
  56. mo_hint_experimental,
  57. mo_has_deprecated_msg
  58. );
  59. tmoduleoptions = set of tmoduleoption;
  60. tlinkcontaineritem=class(tlinkedlistitem)
  61. public
  62. data : TPathStr;
  63. needlink : cardinal;
  64. constructor Create(const s:TPathStr;m:cardinal);
  65. end;
  66. tlinkcontainer=class(tlinkedlist)
  67. procedure add(const s : TPathStr;m:cardinal);
  68. function get(var m:cardinal) : TPathStr;
  69. function getusemask(mask:cardinal) : TPathStr;
  70. function find(const s:TPathStr):boolean;
  71. end;
  72. tmodule = class;
  73. tused_unit = class;
  74. tunitmaprec = record
  75. u : tmodule;
  76. { number of references }
  77. refs : longint;
  78. { index in the derefmap }
  79. derefidx : longint;
  80. end;
  81. tunitmaparray = array of tunitmaprec;
  82. tderefmaprec = record
  83. u : tmodule;
  84. { modulename, used during ppu load }
  85. modulename : pshortstring;
  86. end;
  87. tderefmaparray = array of tderefmaprec;
  88. { tmodule }
  89. tmodule = class(tmodulebase)
  90. private
  91. FImportLibraryList : TFPHashObjectList;
  92. public
  93. is_reset, { has reset been called ? }
  94. do_reload, { force reloading of the unit }
  95. sources_avail, { if all sources are reachable }
  96. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  97. is_dbginfo_written,
  98. is_unit,
  99. in_interface, { processing the implementation part? }
  100. { allow global settings }
  101. in_global : boolean;
  102. { Whether a mode switch is still allowed at this point in the parsing.}
  103. mode_switch_allowed,
  104. { generate pic helper which loads eip in ecx (for leave procedures) }
  105. requires_ecx_pic_helper,
  106. { generate pic helper which loads eip in ebx (for non leave procedures) }
  107. requires_ebx_pic_helper : boolean;
  108. interface_only: boolean; { interface-only macpas unit; flag does not need saving/restoring to ppu }
  109. mainfilepos : tfileposinfo;
  110. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  111. crc,
  112. interface_crc,
  113. indirect_crc : cardinal;
  114. headerflags : cardinal; { the PPU header flags }
  115. longversion : cardinal; { longer version than what fits in the ppu header }
  116. moduleflags : tmoduleflags; { ppu flags that do not need to be known by just reading the ppu header }
  117. islibrary : boolean; { if it is a library (win32 dll) }
  118. IsPackage : boolean;
  119. change_endian : boolean; { if the unit is loaded on a system with a different endianess than it was compiled on }
  120. moduleid : longint;
  121. unitmap : tunitmaparray; { mapping of all used units }
  122. unitmapsize : longint; { number of units in the map }
  123. derefmap : tderefmaparray; { mapping of all units needed for deref }
  124. derefmapcnt : longint; { number of units in the map }
  125. derefmapsize : longint; { number of units in the map }
  126. derefdataintflen : longint;
  127. derefdata : tdynamicarray;
  128. checkforwarddefs,
  129. deflist,
  130. symlist : TFPObjectList;
  131. forwardgenericdefs : TFPHashObjectList; { contains a list of specializations of a forward declared generic (the key) }
  132. ptrdefs : THashSet; { list of pointerdefs created in this module so we can reuse them (not saved/restored) }
  133. arraydefs : THashSet; { list of single-element-arraydefs created in this module so we can reuse them (not saved/restored) }
  134. procaddrdefs : THashSet; { list of procvardefs created when getting the address of a procdef (not saved/restored) }
  135. {$ifdef llvm}
  136. llvmdefs : THashSet; { defs added for llvm-specific reasons (not saved/restored) }
  137. llvmusedsyms : TFPObjectList; { a list of asmsymbols and their defs that need to be added to llvm.used (so they're not removed by llvm optimisation passes nor by the linker) }
  138. llvmcompilerusedsyms : TFPObjectList; { a list of asmsymbols and their defs that need to be added to llvm.compiler.used (so they're not removed by llvm optimisation passes) }
  139. llvminitprocs,
  140. llvmfiniprocs : TFPList;
  141. llvmmetadatastrings: TFPHashList; { metadata strings (mapping string -> superregister) }
  142. {$endif llvm}
  143. ansistrdef : tobject; { an ansistring def redefined for the current module }
  144. wpoinfo : tunitwpoinfobase; { whole program optimization-related information that is generated during the current run for this unit }
  145. globalsymtable, { pointer to the global symtable of this unit }
  146. localsymtable : TSymtable;{ pointer to the local symtable of this unit }
  147. globalmacrosymtable, { pointer to the global macro symtable of this unit }
  148. localmacrosymtable : TSymtable;{ pointer to the local macro symtable of this unit }
  149. mainscanner : TObject; { scanner object used }
  150. scanner : TObject; { scanner object used }
  151. procinfo : TObject; { current procedure being compiled }
  152. asmdata : TObject; { Assembler data }
  153. asmprefix : pshortstring; { prefix for the smartlink asmfiles }
  154. publicasmsyms : TFPHashObjectList; { contains the assembler symbols which need to be exported from a package }
  155. externasmsyms : TFPHashObjectList; { contains the assembler symbols which are imported from another unit }
  156. unitimportsyms : tfpobjectlist; { list of symbols that are imported from other units }
  157. debuginfo : TObject;
  158. _exports : tlinkedlist;
  159. dllscannerinputlist : TFPHashList;
  160. localnamespacelist,
  161. resourcefiles,
  162. linkorderedsymbols : TCmdStrList;
  163. linkunitofiles,
  164. linkunitstaticlibs,
  165. linkunitsharedlibs,
  166. linkotherofiles, { objects,libs loaded from the source }
  167. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  168. linkotherstaticlibs,
  169. linkotherframeworks : tlinkcontainer;
  170. mainname : pshortstring; { alternate name for "main" procedure }
  171. package : tpackage;
  172. used_units : tlinkedlist;
  173. dependent_units : tlinkedlist;
  174. localunitsearchpath, { local searchpaths }
  175. localobjectsearchpath,
  176. localincludesearchpath,
  177. locallibrarysearchpath,
  178. localframeworksearchpath : TSearchPathList;
  179. moduleoptions: tmoduleoptions;
  180. deprecatedmsg: pshortstring;
  181. loadcount : integer;
  182. compilecount : integer;
  183. consume_semicolon_after_uses : Boolean;
  184. initfinalchecked : boolean;
  185. functypechecked : boolean;
  186. { contains a list of types that are extended by helper types; the key is
  187. the full name of the type and the data is a TFPObjectList of
  188. tobjectdef instances (the helper defs) }
  189. extendeddefs: TFPHashObjectList;
  190. { contains a list of the current topmost non-generic symbol for a
  191. typename of which at least one generic exists; the key is the
  192. non-generic typename and the data is a TFPObjectList of tgenericdummyentry
  193. instances whereby the last one is the current top most one }
  194. genericdummysyms: TFPHashObjectList;
  195. { contains a list of specializations for which the method bodies need
  196. to be generated }
  197. pendingspecializations : TFPHashObjectList;
  198. { list of attributes that are used and thus need their construction
  199. functions generated }
  200. used_rtti_attrs: tfpobjectlist;
  201. { this contains a list of units that needs to be waited for until the
  202. unit can be finished (code generated, etc.); this is needed to handle
  203. specializations in circular unit usages correctly }
  204. waitingforunit: tfpobjectlist;
  205. { this contains a list of all units that are waiting for this unit to be
  206. finished }
  207. waitingunits: tfpobjectlist;
  208. finishstate: pointer;
  209. specializestate : pointer;
  210. namespace: pshortstring; { for JVM target: corresponds to Java package name }
  211. { for targets that initialise typed constants via explicit assignments
  212. instead of by generating an initialised data section (holds typed
  213. constant assignments at the module level; does not have to be saved
  214. into the ppu file, because translated into code during compilation)
  215. -- actual type: tnode (but fmodule should not depend on node) }
  216. tcinitcode : tobject;
  217. { the current extended rtti directive }
  218. rtti_directive : trtti_directive;
  219. {create creates a new module which name is stored in 's'. LoadedFrom
  220. points to the module calling it. It is nil for the first compiled
  221. module. This allow inheritence of all path lists. MUST pay attention
  222. to that when creating link.res!!!!(mazen)}
  223. constructor create(LoadedFrom:TModule;const amodulename: string; const afilename:TPathStr;_is_unit:boolean);
  224. destructor destroy;override;
  225. procedure reset(for_recompile: boolean);virtual;
  226. procedure loadlocalnamespacelist;
  227. procedure adddependency(callermodule:tmodule; frominterface : boolean);
  228. procedure flagdependent(callermodule:tmodule);
  229. procedure addimportedsym(sym:TSymEntry);
  230. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  231. function usesmodule_in_interface(m : tmodule) : boolean;
  232. function findusedunit(m : tmodule) : tused_unit;
  233. function usedunitsloaded(interface_units: boolean; out firstwaiting : tmodule): boolean;
  234. function nowaitingforunits(out firstwaiting : tmodule) : Boolean;
  235. procedure updatemaps;
  236. function derefidx_unit(id:longint):longint;
  237. function resolve_unit(id:longint):tmodule;
  238. procedure allunitsused;
  239. procedure end_of_parsing;virtual;
  240. procedure setmodulename(const s:string);
  241. procedure AddExternalImport(const libname,symname,symmangledname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  242. procedure add_public_asmsym(sym:TAsmSymbol);
  243. procedure add_public_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  244. procedure add_extern_asmsym(sym:TAsmSymbol);
  245. procedure add_extern_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  246. procedure remove_from_waitingforunits(amodule : tmodule);
  247. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  248. function ToString: RTLString; override;
  249. end;
  250. { tused_unit }
  251. tused_unit = class(tlinkedlistitem)
  252. checksum,
  253. interface_checksum,
  254. indirect_checksum: cardinal;
  255. in_uses,
  256. in_interface : boolean;
  257. u : tmodule;
  258. unitsym : tunitsym;
  259. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  260. procedure check_hints;
  261. end;
  262. tdependent_unit = class(tlinkedlistitem)
  263. u : tmodule;
  264. in_interface : boolean;
  265. constructor create(_u : tmodule; frominterface : boolean);
  266. end;
  267. var
  268. main_module : tmodule; { Main module of the program }
  269. current_module : tmodule; { Current module which is compiled or loaded }
  270. compiled_module : tmodule; { Current module which is compiled }
  271. usedunits : tlinkedlist; { Used units for this program }
  272. loaded_units : tlinkedlist; { All loaded units }
  273. unloaded_units : tlinkedlist; { Units removed from loaded_units, to be freed }
  274. SmartLinkOFiles : TCmdStrList; { List of .o files which are generated,
  275. used to delete them after linking }
  276. procedure set_current_module(p:tmodule);
  277. function get_module(moduleindex : longint) : tmodule;
  278. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  279. procedure addloadedunit(hp:tmodule);
  280. function find_module_from_symtable(st:tsymtable):tmodule;
  281. implementation
  282. uses
  283. SysUtils,globals,
  284. verbose,systems,
  285. scanner,ppu,dbgbase,
  286. procinfo,symdef,symtype;
  287. {$ifdef MEMDEBUG}
  288. var
  289. memsymtable : TMemDebug;
  290. {$endif}
  291. {*****************************************************************************
  292. Global Functions
  293. *****************************************************************************}
  294. function find_module_from_symtable(st:tsymtable):tmodule;
  295. var
  296. hp : tmodule;
  297. begin
  298. result:=nil;
  299. hp:=tmodule(loaded_units.first);
  300. while assigned(hp) do
  301. begin
  302. if (hp.moduleid=st.moduleid) then
  303. begin
  304. result:=hp;
  305. exit;
  306. end;
  307. hp:=tmodule(hp.next);
  308. end;
  309. end;
  310. procedure set_current_module(p:tmodule);
  311. begin
  312. { save the state of the scanner }
  313. if assigned(current_scanner) then
  314. current_scanner.tempcloseinputfile;
  315. { set new module }
  316. current_module:=p;
  317. { restore previous module settings }
  318. Fillchar(current_filepos,sizeof(current_filepos),0);
  319. if assigned(current_module) then
  320. begin
  321. current_asmdata:=tasmdata(current_module.asmdata);
  322. current_debuginfo:=tdebuginfo(current_module.debuginfo);
  323. { restore scanner and file positions }
  324. set_current_scanner(tscannerfile(current_module.scanner));
  325. if assigned(current_scanner) then
  326. begin
  327. current_scanner.tempopeninputfile;
  328. current_scanner.gettokenpos;
  329. parser_current_file:=current_scanner.inputfile.name;
  330. end
  331. else
  332. begin
  333. current_filepos.moduleindex:=current_module.unit_index;
  334. parser_current_file:='';
  335. end;
  336. end
  337. else
  338. begin
  339. current_asmdata:=nil;
  340. set_current_scanner(nil);
  341. current_debuginfo:=nil;
  342. end;
  343. end;
  344. function get_module(moduleindex : longint) : tmodule;
  345. var
  346. hp : tmodule;
  347. begin
  348. result:=nil;
  349. if moduleindex=0 then
  350. exit;
  351. result:=current_module;
  352. if not(assigned(loaded_units)) then
  353. exit;
  354. hp:=tmodule(loaded_units.first);
  355. while assigned(hp) and (hp.unit_index<>moduleindex) do
  356. hp:=tmodule(hp.next);
  357. result:=hp;
  358. end;
  359. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  360. var
  361. hp : tmodule;
  362. begin
  363. hp:=get_module(moduleindex);
  364. if assigned(hp) then
  365. get_source_file:=hp.sourcefiles.get_file(fileindex)
  366. else
  367. get_source_file:=nil;
  368. end;
  369. procedure addloadedunit(hp:tmodule);
  370. begin
  371. hp.moduleid:=loaded_units.count;
  372. loaded_units.concat(hp);
  373. end;
  374. {****************************************************************************
  375. TLinkContainerItem
  376. ****************************************************************************}
  377. constructor TLinkContainerItem.Create(const s:TPathStr;m:cardinal);
  378. begin
  379. inherited Create;
  380. data:=s;
  381. needlink:=m;
  382. end;
  383. {****************************************************************************
  384. TLinkContainer
  385. ****************************************************************************}
  386. procedure TLinkContainer.add(const s : TPathStr;m:cardinal);
  387. begin
  388. inherited concat(TLinkContainerItem.Create(s,m));
  389. end;
  390. function TLinkContainer.get(var m:cardinal) : TPathStr;
  391. var
  392. p : tlinkcontaineritem;
  393. begin
  394. p:=tlinkcontaineritem(inherited getfirst);
  395. if p=nil then
  396. begin
  397. get:='';
  398. m:=0;
  399. end
  400. else
  401. begin
  402. get:=p.data;
  403. m:=p.needlink;
  404. p.free;
  405. end;
  406. end;
  407. function TLinkContainer.getusemask(mask:cardinal) : TPathStr;
  408. var
  409. p : tlinkcontaineritem;
  410. found : boolean;
  411. begin
  412. found:=false;
  413. repeat
  414. p:=tlinkcontaineritem(inherited getfirst);
  415. if p=nil then
  416. begin
  417. getusemask:='';
  418. exit;
  419. end;
  420. getusemask:=p.data;
  421. found:=(p.needlink and mask)<>0;
  422. p.free;
  423. until found;
  424. end;
  425. function TLinkContainer.find(const s:TPathStr):boolean;
  426. var
  427. newnode : tlinkcontaineritem;
  428. begin
  429. find:=false;
  430. newnode:=tlinkcontaineritem(First);
  431. while assigned(newnode) do
  432. begin
  433. if newnode.data=s then
  434. begin
  435. find:=true;
  436. exit;
  437. end;
  438. newnode:=tlinkcontaineritem(newnode.next);
  439. end;
  440. end;
  441. {****************************************************************************
  442. TUSED_UNIT
  443. ****************************************************************************}
  444. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  445. begin
  446. u:=_u;
  447. in_interface:=intface;
  448. in_uses:=inuses;
  449. unitsym:=usym;
  450. if _u.state in [ms_compiled,ms_processed] then
  451. begin
  452. checksum:=u.crc;
  453. interface_checksum:=u.interface_crc;
  454. indirect_checksum:=u.indirect_crc;
  455. end
  456. else
  457. begin
  458. checksum:=0;
  459. interface_checksum:=0;
  460. indirect_checksum:=0;
  461. end;
  462. end;
  463. procedure tused_unit.check_hints;
  464. var
  465. uname: pshortstring;
  466. begin
  467. uname:=u.realmodulename;
  468. if mo_hint_deprecated in u.moduleoptions then
  469. if (mo_has_deprecated_msg in u.moduleoptions) and (u.deprecatedmsg <> nil) then
  470. MessagePos2(unitsym.fileinfo,sym_w_deprecated_unit_with_msg,uname^,u.deprecatedmsg^)
  471. else
  472. MessagePos1(unitsym.fileinfo,sym_w_deprecated_unit,uname^);
  473. if mo_hint_experimental in u.moduleoptions then
  474. MessagePos1(unitsym.fileinfo,sym_w_experimental_unit,uname^);
  475. if mo_hint_platform in u.moduleoptions then
  476. MessagePos1(unitsym.fileinfo,sym_w_non_portable_unit,uname^);
  477. if mo_hint_library in u.moduleoptions then
  478. MessagePos1(unitsym.fileinfo,sym_w_library_unit,uname^);
  479. if mo_hint_unimplemented in u.moduleoptions then
  480. MessagePos1(unitsym.fileinfo,sym_w_non_implemented_unit,uname^);
  481. end;
  482. {****************************************************************************
  483. TDENPENDENT_UNIT
  484. ****************************************************************************}
  485. constructor tdependent_unit.create(_u: tmodule; frominterface: boolean);
  486. begin
  487. u:=_u;
  488. in_interface:=frominterface;
  489. end;
  490. {****************************************************************************
  491. TMODULE
  492. ****************************************************************************}
  493. constructor tmodule.create(LoadedFrom:TModule;const amodulename: string; const afilename:TPathStr;_is_unit:boolean);
  494. var
  495. n:string;
  496. fn:TPathStr;
  497. begin
  498. if amodulename='' then
  499. n:=ChangeFileExt(ExtractFileName(afilename),'')
  500. else
  501. n:=amodulename;
  502. if afilename='' then
  503. fn:=amodulename
  504. else
  505. fn:=afilename;
  506. { Programs have the name 'Program' to don't conflict with dup id's }
  507. if _is_unit then
  508. inherited create(amodulename)
  509. else
  510. inherited create('Program');
  511. mainsource:=fn;
  512. { Dos has the famous 8.3 limit :( }
  513. {$ifdef shortasmprefix}
  514. asmprefix:=stringdup(FixFileName('as'));
  515. {$else}
  516. asmprefix:=stringdup(FixFileName(n));
  517. {$endif}
  518. setfilename(fn,true);
  519. localunitsearchpath:=TSearchPathList.Create;
  520. localobjectsearchpath:=TSearchPathList.Create;
  521. localincludesearchpath:=TSearchPathList.Create;
  522. locallibrarysearchpath:=TSearchPathList.Create;
  523. localframeworksearchpath:=TSearchPathList.Create;
  524. used_units:=TLinkedList.Create;
  525. dependent_units:=TLinkedList.Create;
  526. localnamespacelist:=TCmdStrList.Create;
  527. resourcefiles:=TCmdStrList.Create;
  528. linkorderedsymbols:=TCmdStrList.Create;
  529. linkunitofiles:=TLinkContainer.Create;
  530. linkunitstaticlibs:=TLinkContainer.Create;
  531. linkunitsharedlibs:=TLinkContainer.Create;
  532. linkotherofiles:=TLinkContainer.Create;
  533. linkotherstaticlibs:=TLinkContainer.Create;
  534. linkothersharedlibs:=TLinkContainer.Create;
  535. linkotherframeworks:=TLinkContainer.Create;
  536. mainname:=nil;
  537. FImportLibraryList:=TFPHashObjectList.Create(true);
  538. crc:=0;
  539. interface_crc:=0;
  540. indirect_crc:=0;
  541. headerflags:=0;
  542. longversion:=0;
  543. moduleflags:=[];
  544. scanner:=nil;
  545. unitmap:=nil;
  546. unitmapsize:=0;
  547. derefmap:=nil;
  548. derefmapsize:=0;
  549. derefmapcnt:=0;
  550. derefdata:=TDynamicArray.Create(1024);
  551. derefdataintflen:=0;
  552. deflist:=TFPObjectList.Create(false);
  553. symlist:=TFPObjectList.Create(false);
  554. ptrdefs:=THashSet.Create(64,true,false);
  555. arraydefs:=THashSet.Create(64,true,false);
  556. procaddrdefs:=THashSet.Create(64,true,false);
  557. {$ifdef llvm}
  558. llvmdefs:=THashSet.Create(64,true,false);
  559. llvmusedsyms:=TFPObjectList.Create(true);
  560. llvmcompilerusedsyms:=TFPObjectList.Create(true);
  561. llvminitprocs:=TFPList.Create;
  562. llvmfiniprocs:=TFPList.Create;
  563. llvmmetadatastrings:=TFPHashList.Create;
  564. {$endif llvm}
  565. ansistrdef:=nil;
  566. wpoinfo:=nil;
  567. checkforwarddefs:=TFPObjectList.Create(false);
  568. forwardgenericdefs:=TFPHashObjectList.Create(true);
  569. extendeddefs:=TFPHashObjectList.Create(true);
  570. genericdummysyms:=tfphashobjectlist.create(true);
  571. pendingspecializations:=tfphashobjectlist.create(false);
  572. waitingforunit:=tfpobjectlist.create(false);
  573. waitingunits:=tfpobjectlist.create(false);
  574. used_rtti_attrs:=tfpobjectlist.create(false);
  575. globalsymtable:=nil;
  576. localsymtable:=nil;
  577. globalmacrosymtable:=nil;
  578. localmacrosymtable:=nil;
  579. do_reload:=false;
  580. sources_avail:=true;
  581. mainfilepos.line:=0;
  582. mainfilepos.column:=0;
  583. mainfilepos.fileindex:=0;
  584. recompile_reason:=rr_unknown;
  585. in_interface:=true;
  586. in_global:=true;
  587. is_unit:=_is_unit;
  588. islibrary:=false;
  589. ispackage:=false;
  590. change_endian:=false;
  591. is_dbginfo_written:=false;
  592. mode_switch_allowed:= true;
  593. moduleoptions:=[];
  594. deprecatedmsg:=nil;
  595. namespace:=nil;
  596. tcinitcode:=nil;
  597. _exports:=TLinkedList.Create;
  598. dllscannerinputlist:=TFPHashList.Create;
  599. asmdata:=casmdata.create(modulename);
  600. unitimportsyms:=TFPObjectList.Create(false);
  601. publicasmsyms:=TFPHashObjectList.Create(true);
  602. externasmsyms:=TFPHashObjectList.Create(true);
  603. InitDebugInfo(self,false);
  604. end;
  605. destructor tmodule.destroy;
  606. var
  607. i : longint;
  608. current_debuginfo_reset : boolean;
  609. begin
  610. unitmap:=nil;
  611. if assigned(derefmap) then
  612. begin
  613. for i:=0 to derefmapcnt-1 do
  614. stringdispose(derefmap[i].modulename);
  615. end;
  616. derefmap:=nil;
  617. if assigned(_exports) then
  618. _exports.free;
  619. if assigned(dllscannerinputlist) then
  620. dllscannerinputlist.free;
  621. if assigned(scanner) then
  622. begin
  623. { also update current_scanner if it was pointing
  624. to this module }
  625. if current_scanner=tscannerfile(scanner) then
  626. set_current_scanner(nil);
  627. freeandnil(scanner);
  628. end;
  629. if assigned(asmdata) then
  630. begin
  631. if current_asmdata=asmdata then
  632. current_asmdata:=nil;
  633. asmdata.free;
  634. end;
  635. if assigned(procinfo) then
  636. begin
  637. if current_procinfo=tprocinfo(procinfo) then
  638. begin
  639. current_procinfo:=nil;
  640. current_structdef:=nil;
  641. current_genericdef:=nil;
  642. current_specializedef:=nil;
  643. end;
  644. { release procinfo tree }
  645. tprocinfo(procinfo).destroy_tree;
  646. procinfo := nil;
  647. end;
  648. DoneDebugInfo(self,current_debuginfo_reset);
  649. used_units.free;
  650. dependent_units.free;
  651. resourcefiles.Free;
  652. linkorderedsymbols.Free;
  653. linkunitofiles.Free;
  654. linkunitstaticlibs.Free;
  655. linkunitsharedlibs.Free;
  656. linkotherofiles.Free;
  657. linkotherstaticlibs.Free;
  658. linkothersharedlibs.Free;
  659. linkotherframeworks.Free;
  660. stringdispose(mainname);
  661. externasmsyms.Free;
  662. publicasmsyms.Free;
  663. unitimportsyms.Free;
  664. FImportLibraryList.Free;
  665. extendeddefs.Free;
  666. genericdummysyms.free;
  667. pendingspecializations.free;
  668. waitingforunit.free;
  669. waitingunits.free;
  670. used_rtti_attrs.free;
  671. stringdispose(asmprefix);
  672. stringdispose(deprecatedmsg);
  673. stringdispose(namespace);
  674. tcinitcode.free;
  675. localunitsearchpath.Free;
  676. localobjectsearchpath.free;
  677. localincludesearchpath.free;
  678. locallibrarysearchpath.free;
  679. localframeworksearchpath.free;
  680. {$ifdef MEMDEBUG}
  681. memsymtable.start;
  682. {$endif}
  683. derefdata.free;
  684. if assigned(deflist) then
  685. begin
  686. for i:=0 to deflist.Count-1 do
  687. if assigned(deflist[i]) and
  688. (tdef(deflist[i]).registered_in_module=self) then
  689. tdef(deflist[i]).registered_in_module:=nil;
  690. deflist.free;
  691. end;
  692. symlist.free;
  693. ptrdefs.free;
  694. arraydefs.free;
  695. procaddrdefs.free;
  696. {$ifdef llvm}
  697. llvmdefs.free;
  698. llvmusedsyms.free;
  699. llvmcompilerusedsyms.free;
  700. llvminitprocs.free;
  701. llvmfiniprocs.free;
  702. llvmmetadatastrings.free;
  703. {$endif llvm}
  704. ansistrdef:=nil;
  705. wpoinfo.free;
  706. checkforwarddefs.free;
  707. forwardgenericdefs.free;
  708. globalsymtable.free;
  709. localsymtable.free;
  710. globalmacrosymtable.free;
  711. localmacrosymtable.free;
  712. {$ifdef MEMDEBUG}
  713. memsymtable.stop;
  714. {$endif}
  715. inherited Destroy;
  716. end;
  717. procedure tmodule.reset(for_recompile: boolean);
  718. var
  719. i : longint;
  720. current_debuginfo_reset : boolean;
  721. m : tmodule;
  722. begin
  723. is_reset:=true;
  724. LoadCount:=0;
  725. if assigned(scanner) then
  726. begin
  727. { also update current_scanner if it was pointing
  728. to this module }
  729. if current_scanner=tscannerfile(scanner) then
  730. set_current_scanner(nil);
  731. freeandnil(scanner);
  732. end;
  733. if assigned(procinfo) then
  734. begin
  735. if current_procinfo=tprocinfo(procinfo) then
  736. begin
  737. current_procinfo:=nil;
  738. current_structdef:=nil;
  739. current_genericdef:=nil;
  740. current_specializedef:=nil;
  741. end;
  742. { release procinfo tree }
  743. tprocinfo(procinfo).destroy_tree;
  744. end;
  745. if assigned(asmdata) then
  746. begin
  747. if current_asmdata=asmdata then
  748. current_asmdata:=nil;
  749. asmdata.free;
  750. asmdata:=nil;
  751. end;
  752. DoneDebugInfo(self,current_debuginfo_reset);
  753. globalsymtable.free;
  754. globalsymtable:=nil;
  755. localsymtable.free;
  756. localsymtable:=nil;
  757. globalmacrosymtable.free;
  758. globalmacrosymtable:=nil;
  759. localmacrosymtable.free;
  760. localmacrosymtable:=nil;
  761. deflist.free;
  762. deflist:=TFPObjectList.Create(false);
  763. symlist.free;
  764. symlist:=TFPObjectList.Create(false);
  765. ptrdefs.free;
  766. ptrdefs:=THashSet.Create(64,true,false);
  767. arraydefs.free;
  768. arraydefs:=THashSet.Create(64,true,false);
  769. procaddrdefs.free;
  770. procaddrdefs:=THashSet.Create(64,true,false);
  771. {$ifdef llvm}
  772. llvmdefs.free;
  773. llvmdefs:=THashSet.Create(64,true,false);
  774. llvmusedsyms.free;
  775. llvmusedsyms:=TFPObjectList.Create(true);
  776. llvmcompilerusedsyms.free;
  777. llvmcompilerusedsyms:=TFPObjectList.Create(true);
  778. llvminitprocs.free;
  779. llvminitprocs:=TFPList.Create;
  780. llvmfiniprocs.free;
  781. llvmfiniprocs:=TFPList.Create;
  782. llvmmetadatastrings.free;
  783. llvmmetadatastrings:=TFPHashList.Create;
  784. {$endif llvm}
  785. ansistrdef:=nil;
  786. wpoinfo.free;
  787. wpoinfo:=nil;
  788. checkforwarddefs.free;
  789. checkforwarddefs:=TFPObjectList.Create(false);
  790. forwardgenericdefs.free;
  791. forwardgenericdefs:=TFPHashObjectList.Create(true);
  792. publicasmsyms.free;
  793. publicasmsyms:=TFPHashObjectList.Create(true);
  794. externasmsyms.free;
  795. externasmsyms:=TFPHashObjectList.Create(true);
  796. unitimportsyms.free;
  797. unitimportsyms:=TFPObjectList.Create(false);
  798. derefdata.free;
  799. derefdata:=TDynamicArray.Create(1024);
  800. unitmap:=nil;
  801. if assigned(derefmap) then
  802. begin
  803. for i:=0 to derefmapcnt-1 do
  804. stringdispose(derefmap[i].modulename);
  805. end;
  806. derefmap:=nil;
  807. unitmapsize:=0;
  808. derefmapsize:=0;
  809. derefmapcnt:=0;
  810. derefdataintflen:=0;
  811. sourcefiles.free;
  812. sourcefiles:=tinputfilemanager.create;
  813. asmdata:=casmdata.create(modulename);
  814. InitDebugInfo(self,current_debuginfo_reset);
  815. _exports.free;
  816. _exports:=tlinkedlist.create;
  817. dllscannerinputlist.free;
  818. dllscannerinputlist:=TFPHashList.create;
  819. { During reload, the list of used units cannot change.
  820. It can only change while recompiling.
  821. Because the used_units is used in loops in the load cycle(s) which
  822. can recurse into the same unit due to circular dependencies,
  823. we do not destroy the list, we only update the contents.
  824. As a result so the loop variable does not get reset during the loop.
  825. For recompile, we recreate the list }
  826. if for_recompile then
  827. begin
  828. used_units.free;
  829. used_units:=TLinkedList.Create;
  830. end;
  831. dependent_units.free;
  832. dependent_units:=TLinkedList.Create;
  833. resourcefiles.Free;
  834. resourcefiles:=TCmdStrList.Create;
  835. linkorderedsymbols.Free;
  836. linkorderedsymbols:=TCmdStrList.Create;
  837. pendingspecializations.free;
  838. pendingspecializations:=tfphashobjectlist.create(false);
  839. genericdummysyms.Free;
  840. genericdummysyms := tfphashobjectlist.create(true);
  841. extendeddefs.Free;
  842. extendeddefs:=TFPHashObjectList.Create(true);
  843. if assigned(waitingforunit) and
  844. (waitingforunit.count<>0) then
  845. begin
  846. Write(Self.modulename^, ' is reset while still waiting for units: ');
  847. for I:=0 to waitingforunit.Count-1 do
  848. begin
  849. M:=tmodule(waitingforunit.Items[i]);
  850. write(m.modulename^,' (state:',M.state,') ');
  851. end;
  852. Writeln;
  853. internalerror(2016070501);
  854. end;
  855. waitingforunit.free;
  856. waitingforunit:=tfpobjectlist.create(false);
  857. linkunitofiles.Free;
  858. linkunitofiles:=TLinkContainer.Create;
  859. linkunitstaticlibs.Free;
  860. linkunitstaticlibs:=TLinkContainer.Create;
  861. linkunitsharedlibs.Free;
  862. linkunitsharedlibs:=TLinkContainer.Create;
  863. linkotherofiles.Free;
  864. linkotherofiles:=TLinkContainer.Create;
  865. linkotherstaticlibs.Free;
  866. linkotherstaticlibs:=TLinkContainer.Create;
  867. linkothersharedlibs.Free;
  868. linkothersharedlibs:=TLinkContainer.Create;
  869. linkotherframeworks.Free;
  870. linkotherframeworks:=TLinkContainer.Create;
  871. stringdispose(mainname);
  872. FImportLibraryList.Free;
  873. FImportLibraryList:=TFPHashObjectList.Create;
  874. do_reload:=false;
  875. interface_compiled:=false;
  876. in_interface:=true;
  877. in_global:=true;
  878. mode_switch_allowed:=true;
  879. stringdispose(deprecatedmsg);
  880. stringdispose(namespace);
  881. tcinitcode.free;
  882. tcinitcode:=nil;
  883. localunitsearchpath.Free;
  884. localunitsearchpath:=TSearchPathList.Create;
  885. localobjectsearchpath.free;
  886. localobjectsearchpath:=TSearchPathList.Create;
  887. localincludesearchpath.free;
  888. localincludesearchpath:=TSearchPathList.Create;
  889. locallibrarysearchpath.free;
  890. locallibrarysearchpath:=TSearchPathList.Create;
  891. localframeworksearchpath.free;
  892. localframeworksearchpath:=TSearchPathList.Create;
  893. moduleoptions:=[];
  894. is_dbginfo_written:=false;
  895. crc:=0;
  896. interface_crc:=0;
  897. indirect_crc:=0;
  898. headerflags:=0;
  899. longversion:=0;
  900. moduleflags:=[];
  901. mainfilepos.line:=0;
  902. mainfilepos.column:=0;
  903. mainfilepos.fileindex:=0;
  904. recompile_reason:=rr_unknown;
  905. {
  906. The following fields should not
  907. be reset:
  908. mainsource
  909. state
  910. loaded_from
  911. sources_avail
  912. }
  913. end;
  914. procedure tmodule.loadlocalnamespacelist;
  915. var
  916. nsitem : TCmdStrListItem;
  917. begin
  918. // Copying local namespace list
  919. if premodule_namespacelist.Count>0 then
  920. begin
  921. nsitem:=TCmdStrListItem(premodule_namespacelist.First);
  922. while assigned(nsItem) do
  923. begin
  924. localnamespacelist.Concat(nsitem.Str);
  925. nsItem:=TCmdStrListItem(nsitem.Next);
  926. end;
  927. premodule_namespacelist.Clear;
  928. end;
  929. current_namespacelist:=localnamespacelist;
  930. end;
  931. procedure tmodule.adddependency(callermodule: tmodule; frominterface: boolean);
  932. begin
  933. { This is not needed for programs }
  934. if not callermodule.is_unit then
  935. exit;
  936. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  937. dependent_units.concat(tdependent_unit.create(callermodule,frominterface));
  938. end;
  939. procedure tmodule.flagdependent(callermodule:tmodule);
  940. var
  941. pm : tdependent_unit;
  942. m : tmodule;
  943. begin
  944. { flag all units that depend on this unit for reloading }
  945. pm:=tdependent_unit(current_module.dependent_units.first);
  946. while assigned(pm) do
  947. begin
  948. { We do not have to reload the unit that wants to load
  949. this unit, unless this unit is already compiled during
  950. the loading }
  951. m:=pm.u;
  952. if (m=callermodule) and (m.state<ms_compiled) then
  953. Message1(unit_u_no_reload_is_caller,m.modulename^)
  954. else
  955. if (m.state=ms_compile) {and (pm.u.compilecount>1)} then
  956. Message1(unit_u_no_reload_in_second_compile,m.modulename^)
  957. else
  958. begin
  959. m.do_reload:=true;
  960. Message1(unit_u_flag_for_reload,m.modulename^);
  961. end;
  962. pm:=tdependent_unit(pm.next);
  963. end;
  964. end;
  965. procedure tmodule.addimportedsym(sym:TSymEntry);
  966. begin
  967. if unitimportsyms.IndexOf(sym)<0 then
  968. unitimportsyms.Add(sym);
  969. end;
  970. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  971. var
  972. pu : tused_unit;
  973. begin
  974. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  975. used_units.concat(pu);
  976. addusedunit:=pu;
  977. end;
  978. function tmodule.usedunitsloaded(interface_units : boolean; out firstwaiting : tmodule): boolean;
  979. const
  980. statesneeded : array[boolean] of tmodulestates = ([ms_processed, ms_compiled,ms_compiling_waitimpl, ms_compiling_waitfinish],
  981. [ms_processed, ms_compiled,ms_compiling_waitimpl, ms_compiling_waitfinish]);
  982. var
  983. itm : TLinkedListItem;
  984. states : set of tmodulestate;
  985. begin
  986. Result:=True;
  987. States:=statesneeded[interface_units];
  988. itm:=self.used_units.First;
  989. firstwaiting:=Nil;
  990. while Result and assigned(itm) do
  991. begin
  992. result:=tused_unit(itm).u.state in states;
  993. {$IFDEF DEBUG_CTASK}writeln(' ',ToString,' checking state of ', tused_unit(itm).u.ToString,' : ',tused_unit(itm).u.state,' : ',Result);{$ENDIF}
  994. if not result then
  995. begin
  996. if firstwaiting=Nil then
  997. firstwaiting:=tused_unit(itm).u;
  998. end;
  999. itm:=itm.Next;
  1000. end;
  1001. end;
  1002. function tmodule.nowaitingforunits(out firstwaiting : tmodule): Boolean;
  1003. begin
  1004. firstwaiting:=nil;
  1005. Result:=waitingforunit.count=0;
  1006. If not Result then
  1007. firstwaiting:=tmodule(waitingforunit[0]);
  1008. end;
  1009. function tmodule.usesmodule_in_interface(m: tmodule): boolean;
  1010. var
  1011. u : tused_unit;
  1012. begin
  1013. result:=False;
  1014. u:=tused_unit(used_units.First);
  1015. while assigned(u) do
  1016. begin
  1017. if (u.u=m) then
  1018. exit(u.in_interface) ;
  1019. u:=tused_unit(u.next);
  1020. end;
  1021. end;
  1022. function tmodule.findusedunit(m: tmodule): tused_unit;
  1023. var
  1024. u : tused_unit;
  1025. begin
  1026. result:=nil;
  1027. u:=tused_unit(used_units.First);
  1028. while assigned(u) do
  1029. begin
  1030. if (u.u=m) then
  1031. exit(u);
  1032. u:=tused_unit(u.next);
  1033. end;
  1034. end;
  1035. procedure tmodule.updatemaps;
  1036. var
  1037. oldmapsize : longint;
  1038. hp : tmodule;
  1039. i : longint;
  1040. begin
  1041. { Extend unitmap }
  1042. oldmapsize:=unitmapsize;
  1043. unitmapsize:=loaded_units.count;
  1044. setlength(unitmap,unitmapsize);
  1045. { Extend Derefmap }
  1046. oldmapsize:=derefmapsize;
  1047. derefmapsize:=loaded_units.count;
  1048. setlength(derefmap,derefmapsize);
  1049. { Add all units to unitmap }
  1050. hp:=tmodule(loaded_units.first);
  1051. i:=0;
  1052. while assigned(hp) do
  1053. begin
  1054. if hp.moduleid>=unitmapsize then
  1055. internalerror(200501151);
  1056. { Verify old entries }
  1057. if (i<oldmapsize) then
  1058. begin
  1059. if (hp.moduleid<>i) or
  1060. (unitmap[hp.moduleid].u<>hp) then
  1061. internalerror(200501156);
  1062. end
  1063. else
  1064. begin
  1065. unitmap[hp.moduleid].u:=hp;
  1066. unitmap[hp.moduleid].derefidx:=-1;
  1067. end;
  1068. inc(i);
  1069. hp:=tmodule(hp.next);
  1070. end;
  1071. end;
  1072. function tmodule.derefidx_unit(id:longint):longint;
  1073. begin
  1074. if id>=unitmapsize then
  1075. internalerror(2005011511);
  1076. if unitmap[id].derefidx=-1 then
  1077. begin
  1078. unitmap[id].derefidx:=derefmapcnt;
  1079. inc(derefmapcnt);
  1080. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  1081. end;
  1082. if unitmap[id].derefidx>=derefmapsize then
  1083. internalerror(2005011514);
  1084. result:=unitmap[id].derefidx;
  1085. end;
  1086. function tmodule.resolve_unit(id:longint):tmodule;
  1087. var
  1088. hp : tmodule;
  1089. begin
  1090. if id>=derefmapsize then
  1091. internalerror(200306231);
  1092. result:=derefmap[id].u;
  1093. if not assigned(result) then
  1094. begin
  1095. if not assigned(derefmap[id].modulename) or
  1096. (derefmap[id].modulename^='') then
  1097. internalerror(200501159);
  1098. hp:=tmodule(loaded_units.first);
  1099. while assigned(hp) do
  1100. begin
  1101. { only check for units. The main program is also
  1102. as a unit in the loaded_units list. We simply need
  1103. to ignore this entry (PFV) }
  1104. if hp.is_unit and
  1105. (hp.modulename^=derefmap[id].modulename^) then
  1106. break;
  1107. hp:=tmodule(hp.next);
  1108. end;
  1109. if not assigned(hp) then
  1110. internalerror(2005011510);
  1111. derefmap[id].u:=hp;
  1112. result:=hp;
  1113. end;
  1114. end;
  1115. procedure tmodule.allunitsused;
  1116. var
  1117. pu : tused_unit;
  1118. begin
  1119. pu:=tused_unit(used_units.first);
  1120. while assigned(pu) do
  1121. begin
  1122. if assigned(pu.u.globalsymtable) then
  1123. begin
  1124. if unitmap[pu.u.moduleid].u<>pu.u then
  1125. internalerror(200501157);
  1126. { Give a note when the unit is not referenced, skip
  1127. this is for units with an initialization/finalization }
  1128. if (unitmap[pu.u.moduleid].refs=0) and
  1129. pu.in_uses and
  1130. ((pu.u.moduleflags * [mf_init,mf_finalize])=[]) then
  1131. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  1132. end;
  1133. pu:=tused_unit(pu.next);
  1134. end;
  1135. end;
  1136. procedure tmodule.end_of_parsing;
  1137. begin
  1138. { free asmdata }
  1139. if assigned(asmdata) then
  1140. begin
  1141. asmdata.free;
  1142. asmdata:=nil;
  1143. end;
  1144. { free scanner }
  1145. if assigned(scanner) then
  1146. begin
  1147. if current_scanner=tscannerfile(scanner) then
  1148. set_current_scanner(nil);
  1149. FreeAndNil(scanner);
  1150. scanner:=nil;
  1151. end;
  1152. { free symtable stack }
  1153. if assigned(symtablestack) then
  1154. begin
  1155. symtablestack.free;
  1156. symtablestack:=nil;
  1157. end;
  1158. if assigned(macrosymtablestack) then
  1159. begin
  1160. macrosymtablestack.free;
  1161. macrosymtablestack:=nil;
  1162. end;
  1163. waitingforunit.free;
  1164. waitingforunit:=nil;
  1165. localmacrosymtable.free;
  1166. localmacrosymtable:=nil;
  1167. ptrdefs.free;
  1168. ptrdefs:=nil;
  1169. arraydefs.free;
  1170. arraydefs:=nil;
  1171. procaddrdefs.free;
  1172. procaddrdefs:=nil;
  1173. {$ifdef llvm}
  1174. llvmdefs.free;
  1175. llvmdefs:=nil;
  1176. {$endif llvm}
  1177. checkforwarddefs.free;
  1178. checkforwarddefs:=nil;
  1179. tcinitcode.free;
  1180. tcinitcode:=nil;
  1181. localunitsearchpath.free;
  1182. localunitsearchpath:=nil;
  1183. localobjectsearchpath.free;
  1184. localobjectsearchpath:=nil;
  1185. localincludesearchpath.free;
  1186. localincludesearchpath:=nil;
  1187. locallibrarysearchpath.free;
  1188. locallibrarysearchpath:=nil;
  1189. localframeworksearchpath.free;
  1190. localframeworksearchpath:=nil;
  1191. end;
  1192. procedure tmodule.setmodulename(const s:string);
  1193. begin
  1194. stringdispose(modulename);
  1195. stringdispose(realmodulename);
  1196. modulename:=stringdup(upper(s));
  1197. realmodulename:=stringdup(s);
  1198. { also update asmlibrary names }
  1199. current_asmdata.name:=modulename;
  1200. end;
  1201. procedure tmodule.AddExternalImport(const libname, symname, symmangledname: string; OrdNr: longint; isvar: boolean;
  1202. ImportByOrdinalOnly: boolean);
  1203. var
  1204. ImportLibrary,OtherIL : TImportLibrary;
  1205. ImportSymbol : TImportSymbol;
  1206. i : longint;
  1207. begin
  1208. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  1209. if not assigned(ImportLibrary) then
  1210. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  1211. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  1212. if not assigned(ImportSymbol) then
  1213. begin
  1214. { Check that the same name does not exist in another library }
  1215. { If it does and the same mangled name is used, issue a warning }
  1216. if ImportLibraryList.Count>1 then
  1217. for i:=0 To ImportLibraryList.Count-1 do
  1218. begin
  1219. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  1220. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  1221. if assigned(ImportSymbol) then
  1222. begin
  1223. if ImportSymbol.MangledName=symmangledname then
  1224. begin
  1225. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  1226. break;
  1227. end;
  1228. end;
  1229. end;
  1230. if not ImportByOrdinalOnly then
  1231. { negative ordinal number indicates import by name with ordinal number as hint }
  1232. OrdNr:=-OrdNr;
  1233. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  1234. symname,symmangledname,OrdNr,isvar);
  1235. end;
  1236. end;
  1237. procedure tmodule.add_public_asmsym(sym:TAsmSymbol);
  1238. begin
  1239. add_public_asmsym(sym.name,sym.bind,sym.typ);
  1240. end;
  1241. procedure tmodule.add_public_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1242. var
  1243. sym : tasmsymbol;
  1244. begin
  1245. { ToDo: check for AB_GLOBAL, AB_EXTERNAL? }
  1246. sym:=tasmsymbol(publicasmsyms.find(name));
  1247. if assigned(sym) then
  1248. begin
  1249. if (sym.bind<>bind) or (sym.typ<>typ) then
  1250. internalerror(2016070101);
  1251. exit;
  1252. end;
  1253. tasmsymbol.create(publicasmsyms,name,bind,typ);
  1254. end;
  1255. procedure tmodule.add_extern_asmsym(sym:TAsmSymbol);
  1256. begin
  1257. add_extern_asmsym(sym.name,sym.bind,sym.typ);
  1258. end;
  1259. procedure tmodule.add_extern_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1260. var
  1261. sym : tasmsymbol;
  1262. begin
  1263. { ToDo: check for AB_EXTERNAL? }
  1264. sym:=tasmsymbol(externasmsyms.find(name));
  1265. if assigned(sym) then
  1266. begin
  1267. if (sym.bind<>bind) or (sym.typ<>typ) then
  1268. internalerror(2016070102);
  1269. exit;
  1270. end;
  1271. tasmsymbol.create(externasmsyms,name,bind,typ);
  1272. end;
  1273. procedure tmodule.remove_from_waitingforunits(amodule: tmodule);
  1274. begin
  1275. // It can be nil after when this is called after end_of_parsing was called.
  1276. if assigned(waitingforunit) then
  1277. waitingforunit.remove(amodule);
  1278. end;
  1279. function tmodule.ToString: RTLString;
  1280. begin
  1281. // Assigned self so we can detect nil.
  1282. if assigned(modulename) then
  1283. Result:='('+ModuleName^+')'
  1284. else
  1285. Result:='(<'+inttostr(ptrint(self))+'>)';
  1286. // Possibly add some state ?
  1287. end;
  1288. initialization
  1289. {$ifdef MEMDEBUG}
  1290. memsymtable:=TMemDebug.create('Symtables');
  1291. memsymtable.stop;
  1292. {$endif MEMDEBUG}
  1293. finalization
  1294. {$ifdef MEMDEBUG}
  1295. memsymtable.free;
  1296. {$endif MEMDEBUG}
  1297. end.