fmodule.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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; { 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 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. 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(current_module.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. if (m=callermodule) and (m.state<ms_compiled_waitcrc) then
  1008. Message1(unit_u_no_reload_is_caller,m.modulename^)
  1009. else
  1010. if (m.state=ms_compile) {and (pm.u.compilecount>1)} then
  1011. Message1(unit_u_no_reload_in_second_compile,m.modulename^)
  1012. else
  1013. begin
  1014. m.do_reload:=true;
  1015. Message1(unit_u_flag_for_reload,m.modulename^);
  1016. end;
  1017. pm:=tdependent_unit(pm.next);
  1018. end;
  1019. end;
  1020. procedure tmodule.addimportedsym(sym:TSymEntry);
  1021. begin
  1022. if unitimportsyms.IndexOf(sym)<0 then
  1023. unitimportsyms.Add(sym);
  1024. end;
  1025. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  1026. var
  1027. pu : tused_unit;
  1028. begin
  1029. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  1030. used_units.concat(pu);
  1031. addusedunit:=pu;
  1032. end;
  1033. function tmodule.usedunitsloaded(interface_units : boolean; out firstwaiting : tmodule): boolean;
  1034. const
  1035. statesneeded : array[boolean] of tmodulestates = (
  1036. [ms_processed, ms_compiled, ms_compiling_waitimpl, ms_compiling_waitfinish, ms_compiled_waitcrc],
  1037. [ms_processed, ms_compiled, ms_compiling_waitimpl, ms_compiling_waitfinish, ms_compiled_waitcrc]);
  1038. var
  1039. itm : TLinkedListItem;
  1040. states : set of tmodulestate;
  1041. begin
  1042. Result:=True;
  1043. States:=statesneeded[interface_units];
  1044. itm:=self.used_units.First;
  1045. firstwaiting:=Nil;
  1046. while Result and assigned(itm) do
  1047. begin
  1048. result:=tused_unit(itm).u.state in states;
  1049. {$IFDEF DEBUG_CTASK}writeln(' ',ToString,' checking state of ', tused_unit(itm).u.ToString,' : ',tused_unit(itm).u.state,' : ',Result);{$ENDIF}
  1050. if not result then
  1051. begin
  1052. if firstwaiting=Nil then
  1053. begin
  1054. firstwaiting:=tused_unit(itm).u;
  1055. {$IFNDEF DEBUG_CTASK}
  1056. break;
  1057. {$ENDIF}
  1058. end;
  1059. end;
  1060. itm:=itm.Next;
  1061. end;
  1062. end;
  1063. function tmodule.nowaitingforunits(out firstwaiting : tmodule): Boolean;
  1064. begin
  1065. firstwaiting:=nil;
  1066. Result:=waitingforunit.count=0;
  1067. If not Result then
  1068. firstwaiting:=tmodule(waitingforunit[0]);
  1069. end;
  1070. function tmodule.usedunitsfinalcrc(out firstwaiting: tmodule): boolean;
  1071. var
  1072. itm: TLinkedListItem;
  1073. begin
  1074. Result:=True;
  1075. itm:=self.used_units.First;
  1076. firstwaiting:=Nil;
  1077. while assigned(itm) do
  1078. begin
  1079. if not tused_unit(itm).u.crc_final then
  1080. begin
  1081. firstwaiting:=tused_unit(itm).u;
  1082. exit(false);
  1083. end;
  1084. itm:=itm.Next;
  1085. end;
  1086. end;
  1087. function tmodule.usesmodule_in_interface(m: tmodule): boolean;
  1088. var
  1089. u : tused_unit;
  1090. begin
  1091. result:=False;
  1092. u:=tused_unit(used_units.First);
  1093. while assigned(u) do
  1094. begin
  1095. if (u.u=m) then
  1096. exit(u.in_interface) ;
  1097. u:=tused_unit(u.next);
  1098. end;
  1099. end;
  1100. function tmodule.findusedunit(m: tmodule): tused_unit;
  1101. var
  1102. u : tused_unit;
  1103. begin
  1104. result:=nil;
  1105. u:=tused_unit(used_units.First);
  1106. while assigned(u) do
  1107. begin
  1108. if (u.u=m) then
  1109. exit(u);
  1110. u:=tused_unit(u.next);
  1111. end;
  1112. end;
  1113. procedure tmodule.updatemaps;
  1114. var
  1115. oldmapsize : longint;
  1116. hp : tmodule;
  1117. i : longint;
  1118. begin
  1119. { Extend unitmap }
  1120. oldmapsize:=unitmapsize;
  1121. unitmapsize:=loaded_units.count;
  1122. setlength(unitmap,unitmapsize);
  1123. { Extend Derefmap }
  1124. oldmapsize:=derefmapsize;
  1125. derefmapsize:=loaded_units.count;
  1126. setlength(derefmap,derefmapsize);
  1127. { Add all units to unitmap }
  1128. hp:=tmodule(loaded_units.first);
  1129. i:=0;
  1130. while assigned(hp) do
  1131. begin
  1132. if hp.moduleid>=unitmapsize then
  1133. internalerror(200501151);
  1134. { Verify old entries }
  1135. if (i<oldmapsize) then
  1136. begin
  1137. if (hp.moduleid<>i) or
  1138. (unitmap[hp.moduleid].u<>hp) then
  1139. internalerror(200501156);
  1140. end
  1141. else
  1142. begin
  1143. unitmap[hp.moduleid].u:=hp;
  1144. unitmap[hp.moduleid].derefidx:=-1;
  1145. end;
  1146. inc(i);
  1147. hp:=tmodule(hp.next);
  1148. end;
  1149. end;
  1150. function tmodule.derefidx_unit(id:longint):longint;
  1151. begin
  1152. if id>=unitmapsize then
  1153. internalerror(2005011511);
  1154. if unitmap[id].derefidx=-1 then
  1155. begin
  1156. unitmap[id].derefidx:=derefmapcnt;
  1157. inc(derefmapcnt);
  1158. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  1159. end;
  1160. if unitmap[id].derefidx>=derefmapsize then
  1161. internalerror(2005011514);
  1162. result:=unitmap[id].derefidx;
  1163. end;
  1164. function tmodule.resolve_unit(id:longint):tmodule;
  1165. var
  1166. hp : tmodule;
  1167. begin
  1168. if id>=derefmapsize then
  1169. internalerror(200306231);
  1170. result:=derefmap[id].u;
  1171. if not assigned(result) then
  1172. begin
  1173. if not assigned(derefmap[id].modulename) or
  1174. (derefmap[id].modulename^='') then
  1175. internalerror(200501159);
  1176. hp:=tmodule(loaded_units.first);
  1177. while assigned(hp) do
  1178. begin
  1179. { only check for units. The main program is also
  1180. as a unit in the loaded_units list. We simply need
  1181. to ignore this entry (PFV) }
  1182. if hp.is_unit and
  1183. (hp.modulename^=derefmap[id].modulename^) then
  1184. break;
  1185. hp:=tmodule(hp.next);
  1186. end;
  1187. if not assigned(hp) then
  1188. internalerror(2005011510);
  1189. derefmap[id].u:=hp;
  1190. result:=hp;
  1191. end;
  1192. end;
  1193. procedure tmodule.allunitsused;
  1194. var
  1195. pu : tused_unit;
  1196. begin
  1197. pu:=tused_unit(used_units.first);
  1198. while assigned(pu) do
  1199. begin
  1200. if assigned(pu.u.globalsymtable) then
  1201. begin
  1202. if unitmap[pu.u.moduleid].u<>pu.u then
  1203. internalerror(200501157);
  1204. { Give a note when the unit is not referenced, skip
  1205. this is for units with an initialization/finalization }
  1206. if (unitmap[pu.u.moduleid].refs=0) and
  1207. pu.in_uses and
  1208. ((pu.u.moduleflags * [mf_init,mf_finalize])=[]) then
  1209. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  1210. end;
  1211. pu:=tused_unit(pu.next);
  1212. end;
  1213. end;
  1214. procedure tmodule.end_of_parsing;
  1215. begin
  1216. { free asmdata }
  1217. if assigned(asmdata) then
  1218. begin
  1219. asmdata.free;
  1220. asmdata:=nil;
  1221. end;
  1222. { free scanner }
  1223. if assigned(scanner) then
  1224. begin
  1225. if current_scanner=tscannerfile(scanner) then
  1226. set_current_scanner(nil);
  1227. FreeAndNil(scanner);
  1228. scanner:=nil;
  1229. end;
  1230. { free symtable stack }
  1231. if assigned(symtablestack) then
  1232. begin
  1233. symtablestack.free;
  1234. symtablestack:=nil;
  1235. end;
  1236. if assigned(macrosymtablestack) then
  1237. begin
  1238. macrosymtablestack.free;
  1239. macrosymtablestack:=nil;
  1240. end;
  1241. waitingforunit.free;
  1242. waitingforunit:=nil;
  1243. localmacrosymtable.free;
  1244. localmacrosymtable:=nil;
  1245. ptrdefs.free;
  1246. ptrdefs:=nil;
  1247. arraydefs.free;
  1248. arraydefs:=nil;
  1249. procaddrdefs.free;
  1250. procaddrdefs:=nil;
  1251. {$ifdef llvm}
  1252. llvmdefs.free;
  1253. llvmdefs:=nil;
  1254. {$endif llvm}
  1255. checkforwarddefs.free;
  1256. checkforwarddefs:=nil;
  1257. tcinitcode.free;
  1258. tcinitcode:=nil;
  1259. localunitsearchpath.free;
  1260. localunitsearchpath:=nil;
  1261. localobjectsearchpath.free;
  1262. localobjectsearchpath:=nil;
  1263. localincludesearchpath.free;
  1264. localincludesearchpath:=nil;
  1265. locallibrarysearchpath.free;
  1266. locallibrarysearchpath:=nil;
  1267. localframeworksearchpath.free;
  1268. localframeworksearchpath:=nil;
  1269. end;
  1270. procedure tmodule.setmodulename(const s:string);
  1271. begin
  1272. stringdispose(modulename);
  1273. stringdispose(realmodulename);
  1274. modulename:=stringdup(upper(s));
  1275. realmodulename:=stringdup(s);
  1276. { also update asmlibrary names }
  1277. current_asmdata.name:=modulename;
  1278. end;
  1279. procedure tmodule.AddExternalImport(const libname, symname, symmangledname: string; OrdNr: longint; isvar: boolean;
  1280. ImportByOrdinalOnly: boolean);
  1281. var
  1282. ImportLibrary,OtherIL : TImportLibrary;
  1283. ImportSymbol : TImportSymbol;
  1284. i : longint;
  1285. begin
  1286. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  1287. if not assigned(ImportLibrary) then
  1288. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  1289. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  1290. if not assigned(ImportSymbol) then
  1291. begin
  1292. { Check that the same name does not exist in another library }
  1293. { If it does and the same mangled name is used, issue a warning }
  1294. if ImportLibraryList.Count>1 then
  1295. for i:=0 To ImportLibraryList.Count-1 do
  1296. begin
  1297. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  1298. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  1299. if assigned(ImportSymbol) then
  1300. begin
  1301. if ImportSymbol.MangledName=symmangledname then
  1302. begin
  1303. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  1304. break;
  1305. end;
  1306. end;
  1307. end;
  1308. if not ImportByOrdinalOnly then
  1309. { negative ordinal number indicates import by name with ordinal number as hint }
  1310. OrdNr:=-OrdNr;
  1311. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  1312. symname,symmangledname,OrdNr,isvar);
  1313. end;
  1314. end;
  1315. procedure tmodule.add_public_asmsym(sym:TAsmSymbol);
  1316. begin
  1317. add_public_asmsym(sym.name,sym.bind,sym.typ);
  1318. end;
  1319. procedure tmodule.add_public_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1320. var
  1321. sym : tasmsymbol;
  1322. begin
  1323. { ToDo: check for AB_GLOBAL, AB_EXTERNAL? }
  1324. sym:=tasmsymbol(publicasmsyms.find(name));
  1325. if assigned(sym) then
  1326. begin
  1327. if (sym.bind<>bind) or (sym.typ<>typ) then
  1328. internalerror(2016070101);
  1329. exit;
  1330. end;
  1331. tasmsymbol.create(publicasmsyms,name,bind,typ);
  1332. end;
  1333. procedure tmodule.add_extern_asmsym(sym:TAsmSymbol);
  1334. begin
  1335. add_extern_asmsym(sym.name,sym.bind,sym.typ);
  1336. end;
  1337. procedure tmodule.add_extern_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1338. var
  1339. sym : tasmsymbol;
  1340. begin
  1341. { ToDo: check for AB_EXTERNAL? }
  1342. sym:=tasmsymbol(externasmsyms.find(name));
  1343. if assigned(sym) then
  1344. begin
  1345. if (sym.bind<>bind) or (sym.typ<>typ) then
  1346. internalerror(2016070102);
  1347. exit;
  1348. end;
  1349. tasmsymbol.create(externasmsyms,name,bind,typ);
  1350. end;
  1351. procedure tmodule.remove_from_waitingforunits(amodule: tmodule);
  1352. begin
  1353. // It can be nil after when this is called after end_of_parsing was called.
  1354. if assigned(waitingforunit) then
  1355. waitingforunit.remove(amodule);
  1356. end;
  1357. function tmodule.ToString: RTLString;
  1358. begin
  1359. // Assigned self so we can detect nil.
  1360. if assigned(modulename) then
  1361. Result:='('+ModuleName^+')'
  1362. else
  1363. Result:='(<'+inttostr(ptrint(self))+'>)';
  1364. // Possibly add some state ?
  1365. end;
  1366. initialization
  1367. {$ifdef MEMDEBUG}
  1368. memsymtable:=TMemDebug.create('Symtables');
  1369. memsymtable.stop;
  1370. {$endif MEMDEBUG}
  1371. finalization
  1372. {$ifdef MEMDEBUG}
  1373. memsymtable.free;
  1374. memsymtable := nil;
  1375. {$endif MEMDEBUG}
  1376. end.