2
0

fmodule.pas 49 KB

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