fmodule.pas 46 KB

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