fmodule.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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. { Although 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; { list of tused_unit }
  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 inheritance 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. p := nil;
  408. end;
  409. end;
  410. function TLinkContainer.getusemask(mask:cardinal) : TPathStr;
  411. var
  412. p : tlinkcontaineritem;
  413. found : boolean;
  414. begin
  415. found:=false;
  416. repeat
  417. p:=tlinkcontaineritem(inherited getfirst);
  418. if p=nil then
  419. begin
  420. getusemask:='';
  421. exit;
  422. end;
  423. getusemask:=p.data;
  424. found:=(p.needlink and mask)<>0;
  425. p.free;
  426. p := nil;
  427. until found;
  428. end;
  429. function TLinkContainer.find(const s:TPathStr):boolean;
  430. var
  431. newnode : tlinkcontaineritem;
  432. begin
  433. find:=false;
  434. newnode:=tlinkcontaineritem(First);
  435. while assigned(newnode) do
  436. begin
  437. if newnode.data=s then
  438. begin
  439. find:=true;
  440. exit;
  441. end;
  442. newnode:=tlinkcontaineritem(newnode.next);
  443. end;
  444. end;
  445. {****************************************************************************
  446. TUSED_UNIT
  447. ****************************************************************************}
  448. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  449. begin
  450. u:=_u;
  451. in_interface:=intface;
  452. in_uses:=inuses;
  453. unitsym:=usym;
  454. if _u.state in [ms_compiled_waitcrc,ms_compiled,ms_processed] then
  455. begin
  456. checksum:=u.crc;
  457. interface_checksum:=u.interface_crc;
  458. indirect_checksum:=u.indirect_crc;
  459. end
  460. else
  461. begin
  462. checksum:=0;
  463. interface_checksum:=0;
  464. indirect_checksum:=0;
  465. end;
  466. end;
  467. procedure tused_unit.check_hints;
  468. var
  469. uname: pshortstring;
  470. begin
  471. uname:=u.realmodulename;
  472. if mo_hint_deprecated in u.moduleoptions then
  473. if (mo_has_deprecated_msg in u.moduleoptions) and (u.deprecatedmsg <> nil) then
  474. MessagePos2(unitsym.fileinfo,sym_w_deprecated_unit_with_msg,uname^,u.deprecatedmsg^)
  475. else
  476. MessagePos1(unitsym.fileinfo,sym_w_deprecated_unit,uname^);
  477. if mo_hint_experimental in u.moduleoptions then
  478. MessagePos1(unitsym.fileinfo,sym_w_experimental_unit,uname^);
  479. if mo_hint_platform in u.moduleoptions then
  480. MessagePos1(unitsym.fileinfo,sym_w_non_portable_unit,uname^);
  481. if mo_hint_library in u.moduleoptions then
  482. MessagePos1(unitsym.fileinfo,sym_w_library_unit,uname^);
  483. if mo_hint_unimplemented in u.moduleoptions then
  484. MessagePos1(unitsym.fileinfo,sym_w_non_implemented_unit,uname^);
  485. end;
  486. {****************************************************************************
  487. TDENPENDENT_UNIT
  488. ****************************************************************************}
  489. constructor tdependent_unit.create(_u: tmodule; frominterface: boolean);
  490. begin
  491. u:=_u;
  492. in_interface:=frominterface;
  493. end;
  494. {****************************************************************************
  495. TMODULE
  496. ****************************************************************************}
  497. constructor tmodule.create(LoadedFrom:TModule;const amodulename: string; const afilename:TPathStr;_is_unit:boolean);
  498. var
  499. n:string;
  500. fn:TPathStr;
  501. begin
  502. if amodulename='' then
  503. n:=ChangeFileExt(ExtractFileName(afilename),'')
  504. else
  505. n:=amodulename;
  506. if afilename='' then
  507. fn:=amodulename
  508. else
  509. fn:=afilename;
  510. { Programs have the name 'Program' to don't conflict with dup id's }
  511. if _is_unit then
  512. inherited create(amodulename)
  513. else
  514. inherited create('Program');
  515. mainsource:=fn;
  516. { Dos has the famous 8.3 limit :( }
  517. {$ifdef shortasmprefix}
  518. asmprefix:=stringdup(FixFileName('as'));
  519. {$else}
  520. asmprefix:=stringdup(FixFileName(n));
  521. {$endif}
  522. setfilename(fn,true);
  523. localunitsearchpath:=TSearchPathList.Create;
  524. localobjectsearchpath:=TSearchPathList.Create;
  525. localincludesearchpath:=TSearchPathList.Create;
  526. locallibrarysearchpath:=TSearchPathList.Create;
  527. localframeworksearchpath:=TSearchPathList.Create;
  528. used_units:=TLinkedList.Create;
  529. dependent_units:=TLinkedList.Create;
  530. localnamespacelist:=TCmdStrList.Create;
  531. resourcefiles:=TCmdStrList.Create;
  532. linkorderedsymbols:=TCmdStrList.Create;
  533. linkunitofiles:=TLinkContainer.Create;
  534. linkunitstaticlibs:=TLinkContainer.Create;
  535. linkunitsharedlibs:=TLinkContainer.Create;
  536. linkotherofiles:=TLinkContainer.Create;
  537. linkotherstaticlibs:=TLinkContainer.Create;
  538. linkothersharedlibs:=TLinkContainer.Create;
  539. linkotherframeworks:=TLinkContainer.Create;
  540. mainname:=nil;
  541. FImportLibraryList:=TFPHashObjectList.Create(true);
  542. crc_final:=false;
  543. crc:=0;
  544. interface_crc:=0;
  545. indirect_crc:=0;
  546. headerflags:=0;
  547. longversion:=0;
  548. moduleflags:=[];
  549. scanner:=nil;
  550. unitmap:=nil;
  551. unitmapsize:=0;
  552. derefmap:=nil;
  553. derefmapsize:=0;
  554. derefmapcnt:=0;
  555. derefdata:=TDynamicArray.Create(1024);
  556. derefdataintflen:=0;
  557. deflist:=TFPObjectList.Create(false);
  558. symlist:=TFPObjectList.Create(false);
  559. ptrdefs:=THashSet.Create(64,true,false);
  560. arraydefs:=THashSet.Create(64,true,false);
  561. procaddrdefs:=THashSet.Create(64,true,false);
  562. {$ifdef llvm}
  563. llvmdefs:=THashSet.Create(64,true,false);
  564. llvmusedsyms:=TFPObjectList.Create(true);
  565. llvmcompilerusedsyms:=TFPObjectList.Create(true);
  566. llvminitprocs:=TFPList.Create;
  567. llvmfiniprocs:=TFPList.Create;
  568. llvmmetadatastrings:=TFPHashList.Create;
  569. {$endif llvm}
  570. ansistrdef:=nil;
  571. wpoinfo:=nil;
  572. checkforwarddefs:=TFPObjectList.Create(false);
  573. forwardgenericdefs:=TFPHashObjectList.Create(true);
  574. extendeddefs:=TFPHashObjectList.Create(true);
  575. genericdummysyms:=tfphashobjectlist.create(true);
  576. pendingspecializations:=tfphashobjectlist.create(false);
  577. waitingforunit:=tfpobjectlist.create(false);
  578. waitingunits:=tfpobjectlist.create(false);
  579. used_rtti_attrs:=tfpobjectlist.create(false);
  580. globalsymtable:=nil;
  581. localsymtable:=nil;
  582. globalmacrosymtable:=nil;
  583. localmacrosymtable:=nil;
  584. do_reload:=false;
  585. sources_avail:=true;
  586. mainfilepos.line:=0;
  587. mainfilepos.column:=0;
  588. mainfilepos.fileindex:=0;
  589. recompile_reason:=rr_unknown;
  590. in_interface:=true;
  591. in_global:=true;
  592. is_unit:=_is_unit;
  593. islibrary:=false;
  594. ispackage:=false;
  595. change_endian:=false;
  596. is_dbginfo_written:=false;
  597. mode_switch_allowed:= true;
  598. moduleoptions:=[];
  599. deprecatedmsg:=nil;
  600. namespace:=nil;
  601. tcinitcode:=nil;
  602. _exports:=TLinkedList.Create;
  603. dllscannerinputlist:=TFPHashList.Create;
  604. asmdata:=casmdata.create(modulename);
  605. unitimportsyms:=TFPObjectList.Create(false);
  606. publicasmsyms:=TFPHashObjectList.Create(true);
  607. externasmsyms:=TFPHashObjectList.Create(true);
  608. InitDebugInfo(self,false);
  609. end;
  610. destructor tmodule.destroy;
  611. var
  612. i : longint;
  613. current_debuginfo_reset : boolean;
  614. begin
  615. unitmap:=nil;
  616. if assigned(derefmap) then
  617. begin
  618. for i:=0 to derefmapcnt-1 do
  619. stringdispose(derefmap[i].modulename);
  620. end;
  621. derefmap:=nil;
  622. if assigned(_exports) then
  623. freeandnil(_exports);
  624. if assigned(dllscannerinputlist) then
  625. freeandnil(dllscannerinputlist);
  626. if assigned(localnamespacelist) then
  627. freeandnil(localnamespacelist);
  628. if assigned(scanner) then
  629. begin
  630. { also update current_scanner if it was pointing
  631. to this module }
  632. if current_scanner=tscannerfile(scanner) then
  633. set_current_scanner(nil);
  634. freeandnil(scanner);
  635. end;
  636. if assigned(asmdata) then
  637. begin
  638. if current_asmdata=asmdata then
  639. current_asmdata:=nil;
  640. asmdata.free;
  641. asmdata := nil;
  642. end;
  643. if assigned(procinfo) then
  644. begin
  645. if current_procinfo=tprocinfo(procinfo) then
  646. begin
  647. current_procinfo:=nil;
  648. current_structdef:=nil;
  649. current_genericdef:=nil;
  650. current_specializedef:=nil;
  651. end;
  652. { release procinfo tree }
  653. tprocinfo(procinfo).destroy_tree;
  654. procinfo := nil;
  655. end;
  656. DoneDebugInfo(self,current_debuginfo_reset);
  657. used_units.free;
  658. used_units := nil;
  659. dependent_units.free;
  660. dependent_units := nil;
  661. resourcefiles.Free;
  662. resourcefiles := nil;
  663. linkorderedsymbols.Free;
  664. linkorderedsymbols := nil;
  665. linkunitofiles.Free;
  666. linkunitofiles := nil;
  667. linkunitstaticlibs.Free;
  668. linkunitstaticlibs := nil;
  669. linkunitsharedlibs.Free;
  670. linkunitsharedlibs := nil;
  671. linkotherofiles.Free;
  672. linkotherofiles := nil;
  673. linkotherstaticlibs.Free;
  674. linkotherstaticlibs := nil;
  675. linkothersharedlibs.Free;
  676. linkothersharedlibs := nil;
  677. linkotherframeworks.Free;
  678. linkotherframeworks := nil;
  679. stringdispose(mainname);
  680. externasmsyms.Free;
  681. externasmsyms := nil;
  682. publicasmsyms.Free;
  683. publicasmsyms := nil;
  684. unitimportsyms.Free;
  685. unitimportsyms := nil;
  686. FImportLibraryList.Free;
  687. FImportLibraryList := nil;
  688. extendeddefs.Free;
  689. extendeddefs := nil;
  690. genericdummysyms.free;
  691. genericdummysyms := nil;
  692. pendingspecializations.free;
  693. pendingspecializations := nil;
  694. waitingforunit.free;
  695. waitingforunit := nil;
  696. waitingunits.free;
  697. waitingunits := nil;
  698. used_rtti_attrs.free;
  699. used_rtti_attrs := nil;
  700. stringdispose(asmprefix);
  701. stringdispose(deprecatedmsg);
  702. stringdispose(namespace);
  703. tcinitcode.free;
  704. tcinitcode := nil;
  705. localunitsearchpath.Free;
  706. localunitsearchpath := nil;
  707. localobjectsearchpath.free;
  708. localobjectsearchpath := nil;
  709. localincludesearchpath.free;
  710. localincludesearchpath := nil;
  711. locallibrarysearchpath.free;
  712. locallibrarysearchpath := nil;
  713. localframeworksearchpath.free;
  714. localframeworksearchpath := nil;
  715. {$ifdef MEMDEBUG}
  716. memsymtable.start;
  717. {$endif}
  718. derefdata.free;
  719. derefdata := nil;
  720. if assigned(deflist) then
  721. begin
  722. for i:=0 to deflist.Count-1 do
  723. if assigned(deflist[i]) and
  724. (tdef(deflist[i]).registered_in_module=self) then
  725. tdef(deflist[i]).registered_in_module:=nil;
  726. deflist.free;
  727. deflist := nil;
  728. end;
  729. symlist.free;
  730. symlist := nil;
  731. ptrdefs.free;
  732. ptrdefs := nil;
  733. arraydefs.free;
  734. arraydefs := nil;
  735. procaddrdefs.free;
  736. procaddrdefs := nil;
  737. {$ifdef llvm}
  738. llvmdefs.free;
  739. llvmdefs := nil;
  740. llvmusedsyms.free;
  741. llvmusedsyms := nil;
  742. llvmcompilerusedsyms.free;
  743. llvmcompilerusedsyms := nil;
  744. llvminitprocs.free;
  745. llvminitprocs := nil;
  746. llvmfiniprocs.free;
  747. llvmfiniprocs := nil;
  748. llvmmetadatastrings.free;
  749. llvmmetadatastrings := nil;
  750. {$endif llvm}
  751. ansistrdef:=nil;
  752. wpoinfo.free;
  753. wpoinfo := nil;
  754. checkforwarddefs.free;
  755. checkforwarddefs := nil;
  756. forwardgenericdefs.free;
  757. forwardgenericdefs := nil;
  758. globalsymtable.free;
  759. globalsymtable := nil;
  760. localsymtable.free;
  761. localsymtable := nil;
  762. globalmacrosymtable.free;
  763. globalmacrosymtable := nil;
  764. localmacrosymtable.free;
  765. localmacrosymtable := nil;
  766. {$ifdef MEMDEBUG}
  767. memsymtable.stop;
  768. {$endif}
  769. inherited Destroy;
  770. end;
  771. procedure tmodule.reset(for_recompile: boolean);
  772. var
  773. i : longint;
  774. current_debuginfo_reset : boolean;
  775. m : tmodule;
  776. begin
  777. is_reset:=true;
  778. LoadCount:=0;
  779. if assigned(scanner) then
  780. begin
  781. { also update current_scanner if it was pointing
  782. to this module }
  783. if current_scanner=tscannerfile(scanner) then
  784. set_current_scanner(nil);
  785. freeandnil(scanner);
  786. end;
  787. if assigned(procinfo) then
  788. begin
  789. if current_procinfo=tprocinfo(procinfo) then
  790. begin
  791. current_procinfo:=nil;
  792. current_structdef:=nil;
  793. current_genericdef:=nil;
  794. current_specializedef:=nil;
  795. end;
  796. { release procinfo tree }
  797. tprocinfo(procinfo).destroy_tree;
  798. end;
  799. if assigned(asmdata) then
  800. begin
  801. if current_asmdata=asmdata then
  802. current_asmdata:=nil;
  803. asmdata.free;
  804. asmdata:=nil;
  805. end;
  806. DoneDebugInfo(self,current_debuginfo_reset);
  807. globalsymtable.free;
  808. globalsymtable:=nil;
  809. localsymtable.free;
  810. localsymtable:=nil;
  811. globalmacrosymtable.free;
  812. globalmacrosymtable:=nil;
  813. localmacrosymtable.free;
  814. localmacrosymtable:=nil;
  815. deflist.free;
  816. deflist:=TFPObjectList.Create(false);
  817. symlist.free;
  818. symlist:=TFPObjectList.Create(false);
  819. ptrdefs.free;
  820. ptrdefs:=THashSet.Create(64,true,false);
  821. arraydefs.free;
  822. arraydefs:=THashSet.Create(64,true,false);
  823. procaddrdefs.free;
  824. procaddrdefs:=THashSet.Create(64,true,false);
  825. {$ifdef llvm}
  826. llvmdefs.free;
  827. llvmdefs:=THashSet.Create(64,true,false);
  828. llvmusedsyms.free;
  829. llvmusedsyms:=TFPObjectList.Create(true);
  830. llvmcompilerusedsyms.free;
  831. llvmcompilerusedsyms:=TFPObjectList.Create(true);
  832. llvminitprocs.free;
  833. llvminitprocs:=TFPList.Create;
  834. llvmfiniprocs.free;
  835. llvmfiniprocs:=TFPList.Create;
  836. llvmmetadatastrings.free;
  837. llvmmetadatastrings:=TFPHashList.Create;
  838. {$endif llvm}
  839. ansistrdef:=nil;
  840. wpoinfo.free;
  841. wpoinfo:=nil;
  842. checkforwarddefs.free;
  843. checkforwarddefs:=TFPObjectList.Create(false);
  844. forwardgenericdefs.free;
  845. forwardgenericdefs:=TFPHashObjectList.Create(true);
  846. publicasmsyms.free;
  847. publicasmsyms:=TFPHashObjectList.Create(true);
  848. externasmsyms.free;
  849. externasmsyms:=TFPHashObjectList.Create(true);
  850. unitimportsyms.free;
  851. unitimportsyms:=TFPObjectList.Create(false);
  852. derefdata.free;
  853. derefdata:=TDynamicArray.Create(1024);
  854. unitmap:=nil;
  855. if assigned(derefmap) then
  856. begin
  857. for i:=0 to derefmapcnt-1 do
  858. stringdispose(derefmap[i].modulename);
  859. end;
  860. derefmap:=nil;
  861. unitmapsize:=0;
  862. derefmapsize:=0;
  863. derefmapcnt:=0;
  864. derefdataintflen:=0;
  865. sourcefiles.free;
  866. sourcefiles:=tinputfilemanager.create;
  867. asmdata:=casmdata.create(modulename);
  868. InitDebugInfo(self,current_debuginfo_reset);
  869. _exports.free;
  870. _exports:=tlinkedlist.create;
  871. dllscannerinputlist.free;
  872. dllscannerinputlist:=TFPHashList.create;
  873. { During reload, the list of used units cannot change.
  874. It can only change while recompiling.
  875. Because the used_units is used in loops in the load cycle(s) which
  876. can recurse into the same unit due to circular dependencies,
  877. we do not destroy the list, we only update the contents.
  878. As a result the loop variable does not get reset during the loop.
  879. For recompile, we recreate the list }
  880. if for_recompile then
  881. begin
  882. used_units.free;
  883. used_units:=TLinkedList.Create;
  884. end;
  885. dependent_units.free;
  886. dependent_units:=TLinkedList.Create;
  887. resourcefiles.Free;
  888. resourcefiles:=TCmdStrList.Create;
  889. linkorderedsymbols.Free;
  890. linkorderedsymbols:=TCmdStrList.Create;
  891. pendingspecializations.free;
  892. pendingspecializations:=tfphashobjectlist.create(false);
  893. genericdummysyms.Free;
  894. genericdummysyms := tfphashobjectlist.create(true);
  895. extendeddefs.Free;
  896. extendeddefs:=TFPHashObjectList.Create(true);
  897. if assigned(waitingforunit) and
  898. (waitingforunit.count<>0) then
  899. begin
  900. Write(Self.modulename^, ' is reset while still waiting for units: ');
  901. for I:=0 to waitingforunit.Count-1 do
  902. begin
  903. M:=tmodule(waitingforunit.Items[i]);
  904. write(m.modulename^,' (state:',M.state,') ');
  905. end;
  906. Writeln;
  907. internalerror(2016070501);
  908. end;
  909. waitingforunit.free;
  910. waitingforunit:=tfpobjectlist.create(false);
  911. linkunitofiles.Free;
  912. linkunitofiles:=TLinkContainer.Create;
  913. linkunitstaticlibs.Free;
  914. linkunitstaticlibs:=TLinkContainer.Create;
  915. linkunitsharedlibs.Free;
  916. linkunitsharedlibs:=TLinkContainer.Create;
  917. linkotherofiles.Free;
  918. linkotherofiles:=TLinkContainer.Create;
  919. linkotherstaticlibs.Free;
  920. linkotherstaticlibs:=TLinkContainer.Create;
  921. linkothersharedlibs.Free;
  922. linkothersharedlibs:=TLinkContainer.Create;
  923. linkotherframeworks.Free;
  924. linkotherframeworks:=TLinkContainer.Create;
  925. stringdispose(mainname);
  926. FImportLibraryList.Free;
  927. FImportLibraryList:=TFPHashObjectList.Create;
  928. do_reload:=false;
  929. interface_compiled:=false;
  930. in_interface:=true;
  931. in_global:=true;
  932. mode_switch_allowed:=true;
  933. stringdispose(deprecatedmsg);
  934. stringdispose(namespace);
  935. tcinitcode.free;
  936. tcinitcode:=nil;
  937. localunitsearchpath.Free;
  938. localunitsearchpath:=TSearchPathList.Create;
  939. localobjectsearchpath.free;
  940. localobjectsearchpath:=TSearchPathList.Create;
  941. localincludesearchpath.free;
  942. localincludesearchpath:=TSearchPathList.Create;
  943. locallibrarysearchpath.free;
  944. locallibrarysearchpath:=TSearchPathList.Create;
  945. localframeworksearchpath.free;
  946. localframeworksearchpath:=TSearchPathList.Create;
  947. moduleoptions:=[];
  948. is_dbginfo_written:=false;
  949. crc_final:=false;
  950. crc:=0;
  951. interface_crc:=0;
  952. indirect_crc:=0;
  953. headerflags:=0;
  954. longversion:=0;
  955. moduleflags:=[];
  956. mainfilepos.line:=0;
  957. mainfilepos.column:=0;
  958. mainfilepos.fileindex:=0;
  959. recompile_reason:=rr_unknown;
  960. {
  961. The following fields should not
  962. be reset:
  963. mainsource
  964. state
  965. loaded_from
  966. sources_avail
  967. }
  968. end;
  969. procedure tmodule.loadlocalnamespacelist;
  970. var
  971. nsitem : TCmdStrListItem;
  972. begin
  973. // Copying local namespace list
  974. if premodule_namespacelist.Count>0 then
  975. begin
  976. nsitem:=TCmdStrListItem(premodule_namespacelist.First);
  977. while assigned(nsItem) do
  978. begin
  979. localnamespacelist.Concat(nsitem.Str);
  980. nsItem:=TCmdStrListItem(nsitem.Next);
  981. end;
  982. premodule_namespacelist.Clear;
  983. end;
  984. current_namespacelist:=localnamespacelist;
  985. end;
  986. procedure tmodule.adddependency(callermodule: tmodule; frominterface: boolean);
  987. begin
  988. { This is not needed for programs }
  989. if not callermodule.is_unit then
  990. exit;
  991. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  992. dependent_units.concat(tdependent_unit.create(callermodule,frominterface));
  993. end;
  994. procedure tmodule.flagdependent(callermodule:tmodule);
  995. var
  996. pm : tdependent_unit;
  997. m : tmodule;
  998. begin
  999. { flag all units that depend on this unit for reloading }
  1000. pm:=tdependent_unit(dependent_units.first);
  1001. while assigned(pm) do
  1002. begin
  1003. { We do not have to reload the unit that wants to load
  1004. this unit, unless this unit is already compiled during
  1005. the loading }
  1006. m:=pm.u;
  1007. {$IFDEF DEBUG_PPU_CYCLES}
  1008. writeln('PPUALGO tmodule.flagdependent ',modulename^,' state=',state,', dependent ',m.modulename^,' ',m.state);
  1009. {$ENDIF}
  1010. if (m=callermodule) and (m.state<ms_compiled_waitcrc) then
  1011. Message1(unit_u_no_reload_is_caller,m.modulename^)
  1012. else
  1013. if (m.state=ms_compile) {and (pm.u.compilecount>1)} then
  1014. Message1(unit_u_no_reload_in_second_compile,m.modulename^)
  1015. else
  1016. begin
  1017. m.do_reload:=true;
  1018. Message1(unit_u_flag_for_reload,m.modulename^);
  1019. end;
  1020. pm:=tdependent_unit(pm.next);
  1021. end;
  1022. end;
  1023. procedure tmodule.addimportedsym(sym:TSymEntry);
  1024. begin
  1025. if unitimportsyms.IndexOf(sym)<0 then
  1026. unitimportsyms.Add(sym);
  1027. end;
  1028. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  1029. var
  1030. pu : tused_unit;
  1031. begin
  1032. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  1033. used_units.concat(pu);
  1034. addusedunit:=pu;
  1035. end;
  1036. function tmodule.usedunitsloaded(interface_units : boolean; out firstwaiting : tmodule): boolean;
  1037. const
  1038. statesneeded : array[boolean] of tmodulestates = (
  1039. [ms_processed, ms_compiled, ms_compiling_waitimpl, ms_compiling_waitfinish, ms_compiled_waitcrc],
  1040. [ms_processed, ms_compiled, ms_compiling_waitimpl, ms_compiling_waitfinish, ms_compiled_waitcrc]);
  1041. var
  1042. itm : TLinkedListItem;
  1043. states : set of tmodulestate;
  1044. begin
  1045. Result:=True;
  1046. States:=statesneeded[interface_units];
  1047. itm:=self.used_units.First;
  1048. firstwaiting:=Nil;
  1049. while Result and assigned(itm) do
  1050. begin
  1051. result:=tused_unit(itm).u.state in states;
  1052. {$IFDEF DEBUG_CTASK}writeln(' ',ToString,' checking state of ', tused_unit(itm).u.ToString,' : ',tused_unit(itm).u.state,' : ',Result);{$ENDIF}
  1053. if not result then
  1054. begin
  1055. if firstwaiting=Nil then
  1056. begin
  1057. firstwaiting:=tused_unit(itm).u;
  1058. {$IFNDEF DEBUG_CTASK}
  1059. break;
  1060. {$ENDIF}
  1061. end;
  1062. end;
  1063. itm:=itm.Next;
  1064. end;
  1065. end;
  1066. function tmodule.nowaitingforunits(out firstwaiting : tmodule): Boolean;
  1067. begin
  1068. firstwaiting:=nil;
  1069. Result:=waitingforunit.count=0;
  1070. If not Result then
  1071. firstwaiting:=tmodule(waitingforunit[0]);
  1072. end;
  1073. function tmodule.usedunitsfinalcrc(out firstwaiting: tmodule): boolean;
  1074. var
  1075. itm: TLinkedListItem;
  1076. begin
  1077. Result:=True;
  1078. itm:=self.used_units.First;
  1079. firstwaiting:=Nil;
  1080. while assigned(itm) do
  1081. begin
  1082. if not tused_unit(itm).u.crc_final then
  1083. begin
  1084. firstwaiting:=tused_unit(itm).u;
  1085. exit(false);
  1086. end;
  1087. itm:=itm.Next;
  1088. end;
  1089. end;
  1090. function tmodule.usesmodule_in_interface(m: tmodule): boolean;
  1091. var
  1092. u : tused_unit;
  1093. begin
  1094. result:=False;
  1095. u:=tused_unit(used_units.First);
  1096. while assigned(u) do
  1097. begin
  1098. if (u.u=m) then
  1099. exit(u.in_interface) ;
  1100. u:=tused_unit(u.next);
  1101. end;
  1102. end;
  1103. function tmodule.findusedunit(m: tmodule): tused_unit;
  1104. var
  1105. u : tused_unit;
  1106. begin
  1107. result:=nil;
  1108. u:=tused_unit(used_units.First);
  1109. while assigned(u) do
  1110. begin
  1111. if (u.u=m) then
  1112. exit(u);
  1113. u:=tused_unit(u.next);
  1114. end;
  1115. end;
  1116. procedure tmodule.updatemaps;
  1117. var
  1118. oldmapsize : longint;
  1119. hp : tmodule;
  1120. i : longint;
  1121. begin
  1122. { Extend unitmap }
  1123. oldmapsize:=unitmapsize;
  1124. unitmapsize:=loaded_units.count;
  1125. setlength(unitmap,unitmapsize);
  1126. { Extend Derefmap }
  1127. oldmapsize:=derefmapsize;
  1128. derefmapsize:=loaded_units.count;
  1129. setlength(derefmap,derefmapsize);
  1130. { Add all units to unitmap }
  1131. hp:=tmodule(loaded_units.first);
  1132. i:=0;
  1133. while assigned(hp) do
  1134. begin
  1135. if hp.moduleid>=unitmapsize then
  1136. internalerror(200501151);
  1137. { Verify old entries }
  1138. if (i<oldmapsize) then
  1139. begin
  1140. if (hp.moduleid<>i) or
  1141. (unitmap[hp.moduleid].u<>hp) then
  1142. internalerror(200501156);
  1143. end
  1144. else
  1145. begin
  1146. unitmap[hp.moduleid].u:=hp;
  1147. unitmap[hp.moduleid].derefidx:=-1;
  1148. end;
  1149. inc(i);
  1150. hp:=tmodule(hp.next);
  1151. end;
  1152. end;
  1153. function tmodule.derefidx_unit(id:longint):longint;
  1154. begin
  1155. if id>=unitmapsize then
  1156. internalerror(2005011511);
  1157. if unitmap[id].derefidx=-1 then
  1158. begin
  1159. unitmap[id].derefidx:=derefmapcnt;
  1160. inc(derefmapcnt);
  1161. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  1162. end;
  1163. if unitmap[id].derefidx>=derefmapsize then
  1164. internalerror(2005011514);
  1165. result:=unitmap[id].derefidx;
  1166. end;
  1167. function tmodule.resolve_unit(id:longint):tmodule;
  1168. var
  1169. hp : tmodule;
  1170. begin
  1171. if id>=derefmapsize then
  1172. internalerror(200306231);
  1173. result:=derefmap[id].u;
  1174. if not assigned(result) then
  1175. begin
  1176. if not assigned(derefmap[id].modulename) or
  1177. (derefmap[id].modulename^='') then
  1178. internalerror(200501159);
  1179. hp:=tmodule(loaded_units.first);
  1180. while assigned(hp) do
  1181. begin
  1182. { only check for units. The main program is also
  1183. as a unit in the loaded_units list. We simply need
  1184. to ignore this entry (PFV) }
  1185. if hp.is_unit and
  1186. (hp.modulename^=derefmap[id].modulename^) then
  1187. break;
  1188. hp:=tmodule(hp.next);
  1189. end;
  1190. if not assigned(hp) then
  1191. internalerror(2005011510);
  1192. derefmap[id].u:=hp;
  1193. result:=hp;
  1194. end;
  1195. end;
  1196. procedure tmodule.allunitsused;
  1197. var
  1198. pu : tused_unit;
  1199. begin
  1200. pu:=tused_unit(used_units.first);
  1201. while assigned(pu) do
  1202. begin
  1203. if assigned(pu.u.globalsymtable) then
  1204. begin
  1205. if unitmap[pu.u.moduleid].u<>pu.u then
  1206. internalerror(200501157);
  1207. { Give a note when the unit is not referenced, skip
  1208. this is for units with an initialization/finalization }
  1209. if (unitmap[pu.u.moduleid].refs=0) and
  1210. pu.in_uses and
  1211. ((pu.u.moduleflags * [mf_init,mf_finalize])=[]) then
  1212. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  1213. end;
  1214. pu:=tused_unit(pu.next);
  1215. end;
  1216. end;
  1217. procedure tmodule.end_of_parsing;
  1218. begin
  1219. { free asmdata }
  1220. if assigned(asmdata) then
  1221. begin
  1222. asmdata.free;
  1223. asmdata:=nil;
  1224. end;
  1225. { free scanner }
  1226. if assigned(scanner) then
  1227. begin
  1228. if current_scanner=tscannerfile(scanner) then
  1229. set_current_scanner(nil);
  1230. FreeAndNil(scanner);
  1231. scanner:=nil;
  1232. end;
  1233. { free symtable stack }
  1234. if assigned(symtablestack) then
  1235. begin
  1236. symtablestack.free;
  1237. symtablestack:=nil;
  1238. end;
  1239. if assigned(macrosymtablestack) then
  1240. begin
  1241. macrosymtablestack.free;
  1242. macrosymtablestack:=nil;
  1243. end;
  1244. waitingforunit.free;
  1245. waitingforunit:=nil;
  1246. localmacrosymtable.free;
  1247. localmacrosymtable:=nil;
  1248. ptrdefs.free;
  1249. ptrdefs:=nil;
  1250. arraydefs.free;
  1251. arraydefs:=nil;
  1252. procaddrdefs.free;
  1253. procaddrdefs:=nil;
  1254. {$ifdef llvm}
  1255. llvmdefs.free;
  1256. llvmdefs:=nil;
  1257. {$endif llvm}
  1258. checkforwarddefs.free;
  1259. checkforwarddefs:=nil;
  1260. tcinitcode.free;
  1261. tcinitcode:=nil;
  1262. localunitsearchpath.free;
  1263. localunitsearchpath:=nil;
  1264. localobjectsearchpath.free;
  1265. localobjectsearchpath:=nil;
  1266. localincludesearchpath.free;
  1267. localincludesearchpath:=nil;
  1268. locallibrarysearchpath.free;
  1269. locallibrarysearchpath:=nil;
  1270. localframeworksearchpath.free;
  1271. localframeworksearchpath:=nil;
  1272. end;
  1273. procedure tmodule.setmodulename(const s:string);
  1274. begin
  1275. stringdispose(modulename);
  1276. stringdispose(realmodulename);
  1277. modulename:=stringdup(upper(s));
  1278. realmodulename:=stringdup(s);
  1279. { also update asmlibrary names }
  1280. current_asmdata.name:=modulename;
  1281. end;
  1282. procedure tmodule.AddExternalImport(const libname, symname, symmangledname: string; OrdNr: longint; isvar: boolean;
  1283. ImportByOrdinalOnly: boolean);
  1284. var
  1285. ImportLibrary,OtherIL : TImportLibrary;
  1286. ImportSymbol : TImportSymbol;
  1287. i : longint;
  1288. begin
  1289. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  1290. if not assigned(ImportLibrary) then
  1291. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  1292. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  1293. if not assigned(ImportSymbol) then
  1294. begin
  1295. { Check that the same name does not exist in another library }
  1296. { If it does and the same mangled name is used, issue a warning }
  1297. if ImportLibraryList.Count>1 then
  1298. for i:=0 To ImportLibraryList.Count-1 do
  1299. begin
  1300. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  1301. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  1302. if assigned(ImportSymbol) then
  1303. begin
  1304. if ImportSymbol.MangledName=symmangledname then
  1305. begin
  1306. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  1307. break;
  1308. end;
  1309. end;
  1310. end;
  1311. if not ImportByOrdinalOnly then
  1312. { negative ordinal number indicates import by name with ordinal number as hint }
  1313. OrdNr:=-OrdNr;
  1314. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  1315. symname,symmangledname,OrdNr,isvar);
  1316. end;
  1317. end;
  1318. procedure tmodule.add_public_asmsym(sym:TAsmSymbol);
  1319. begin
  1320. add_public_asmsym(sym.name,sym.bind,sym.typ);
  1321. end;
  1322. procedure tmodule.add_public_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1323. var
  1324. sym : tasmsymbol;
  1325. begin
  1326. { ToDo: check for AB_GLOBAL, AB_EXTERNAL? }
  1327. sym:=tasmsymbol(publicasmsyms.find(name));
  1328. if assigned(sym) then
  1329. begin
  1330. if (sym.bind<>bind) or (sym.typ<>typ) then
  1331. internalerror(2016070101);
  1332. exit;
  1333. end;
  1334. tasmsymbol.create(publicasmsyms,name,bind,typ);
  1335. end;
  1336. procedure tmodule.add_extern_asmsym(sym:TAsmSymbol);
  1337. begin
  1338. add_extern_asmsym(sym.name,sym.bind,sym.typ);
  1339. end;
  1340. procedure tmodule.add_extern_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1341. var
  1342. sym : tasmsymbol;
  1343. begin
  1344. { ToDo: check for AB_EXTERNAL? }
  1345. sym:=tasmsymbol(externasmsyms.find(name));
  1346. if assigned(sym) then
  1347. begin
  1348. if (sym.bind<>bind) or (sym.typ<>typ) then
  1349. internalerror(2016070102);
  1350. exit;
  1351. end;
  1352. tasmsymbol.create(externasmsyms,name,bind,typ);
  1353. end;
  1354. procedure tmodule.remove_from_waitingforunits(amodule: tmodule);
  1355. begin
  1356. // It can be nil after when this is called after end_of_parsing was called.
  1357. if assigned(waitingforunit) then
  1358. waitingforunit.remove(amodule);
  1359. end;
  1360. function tmodule.ToString: RTLString;
  1361. begin
  1362. // Assigned self so we can detect nil.
  1363. if assigned(modulename) then
  1364. Result:='('+ModuleName^+')'
  1365. else
  1366. Result:='(<'+inttostr(ptrint(self))+'>)';
  1367. // Possibly add some state ?
  1368. end;
  1369. initialization
  1370. {$ifdef MEMDEBUG}
  1371. memsymtable:=TMemDebug.create('Symtables');
  1372. memsymtable.stop;
  1373. {$endif MEMDEBUG}
  1374. finalization
  1375. {$ifdef MEMDEBUG}
  1376. memsymtable.free;
  1377. memsymtable := nil;
  1378. {$endif MEMDEBUG}
  1379. end.