import.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman
  4. This unit implements an uniform import object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************}
  17. unit import;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,cclasses,
  22. systems,
  23. aasmbase,
  24. symdef,symsym;
  25. type
  26. timported_item = class(TLinkedListItem)
  27. ordnr : word;
  28. name,
  29. func : pstring;
  30. lab : tasmlabel;
  31. is_var : boolean;
  32. constructor Create(const n,s : string;o : word);
  33. constructor Create_var(const n,s : string);
  34. destructor Destroy;override;
  35. end;
  36. timportlist = class(TLinkedListItem)
  37. dllname : pstring;
  38. imported_items : tlinkedlist;
  39. constructor Create(const n : string);
  40. destructor Destroy;Override;
  41. end;
  42. timportlib=class
  43. private
  44. notsupmsg : boolean;
  45. procedure NotSupported;
  46. public
  47. constructor Create;virtual;
  48. destructor Destroy;override;
  49. procedure preparelib(const s:string);virtual;
  50. procedure importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);virtual;
  51. procedure importvariable(vs:tvarsym;const name,module:string);virtual;
  52. procedure generatelib;virtual;
  53. procedure generatesmartlib;virtual;
  54. end;
  55. TDLLScanner=class
  56. public
  57. f:file;
  58. impname:string;
  59. TheWord:array[0..1]of char;
  60. HeaderOffset:cardinal;
  61. loaded:integer;
  62. function isSuitableFileType(x:cardinal):longbool;virtual;abstract;
  63. function GetEdata(HeaderEntry:cardinal):longbool;virtual;abstract;
  64. function Scan(const binname:string):longbool;virtual;abstract;
  65. end;
  66. TImportLibClass=class of TImportLib;
  67. TDLLScannerClass=class of TDLLScanner;
  68. var
  69. CImportLib : array[tsystem] of TImportLibClass;
  70. CDLLScanner : array[tsystem] of TDLLScannerClass;
  71. ImportLib : TImportLib;
  72. procedure RegisterImport(t:tsystem;c:TImportLibClass);
  73. procedure RegisterDLLScanner(t:tsystem;c:TDLLScannerClass);
  74. procedure InitImport;
  75. procedure DoneImport;
  76. implementation
  77. uses
  78. verbose,globals;
  79. {****************************************************************************
  80. Timported_item
  81. ****************************************************************************}
  82. constructor timported_item.Create(const n,s : string;o : word);
  83. begin
  84. inherited Create;
  85. func:=stringdup(n);
  86. name:=stringdup(s);
  87. ordnr:=o;
  88. lab:=nil;
  89. is_var:=false;
  90. end;
  91. constructor timported_item.create_var(const n,s : string);
  92. begin
  93. inherited Create;
  94. func:=stringdup(n);
  95. name:=stringdup(s);
  96. ordnr:=0;
  97. lab:=nil;
  98. is_var:=true;
  99. end;
  100. destructor timported_item.destroy;
  101. begin
  102. stringdispose(name);
  103. stringdispose(func);
  104. inherited destroy;
  105. end;
  106. {****************************************************************************
  107. TImportlist
  108. ****************************************************************************}
  109. constructor timportlist.Create(const n : string);
  110. begin
  111. inherited Create;
  112. dllname:=stringdup(n);
  113. imported_items:=Tlinkedlist.Create;
  114. end;
  115. destructor timportlist.destroy;
  116. begin
  117. imported_items.free;
  118. stringdispose(dllname);
  119. end;
  120. {****************************************************************************
  121. TImportLib
  122. ****************************************************************************}
  123. constructor timportlib.Create;
  124. begin
  125. notsupmsg:=false;
  126. end;
  127. destructor timportlib.Destroy;
  128. begin
  129. end;
  130. procedure timportlib.NotSupported;
  131. begin
  132. { show the message only once }
  133. if not notsupmsg then
  134. begin
  135. Message(exec_e_dll_not_supported);
  136. notsupmsg:=true;
  137. end;
  138. end;
  139. procedure timportlib.preparelib(const s:string);
  140. begin
  141. NotSupported;
  142. end;
  143. procedure timportlib.importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);
  144. begin
  145. NotSupported;
  146. end;
  147. procedure timportlib.importvariable(vs:tvarsym;const name,module:string);
  148. begin
  149. NotSupported;
  150. end;
  151. procedure timportlib.generatelib;
  152. begin
  153. NotSupported;
  154. end;
  155. procedure timportlib.generatesmartlib;
  156. begin
  157. NotSupported;
  158. end;
  159. {*****************************************************************************
  160. Init/Done
  161. *****************************************************************************}
  162. procedure RegisterImport(t:tsystem;c:TImportLibClass);
  163. begin
  164. CImportLib[t]:=c;
  165. end;
  166. procedure RegisterDLLScanner(t:tsystem;c:TDLLScannerClass);
  167. begin
  168. CDLLScanner[t]:=c;
  169. end;
  170. procedure InitImport;
  171. begin
  172. if assigned(CImportLib[target_info.system]) then
  173. importlib:=CImportLib[target_info.system].Create
  174. else
  175. importlib:=TImportLib.Create;
  176. end;
  177. procedure DoneImport;
  178. begin
  179. if assigned(importlib) then
  180. importlib.free;
  181. end;
  182. end.
  183. {
  184. $Log$
  185. Revision 1.23 2003-04-27 11:21:32 peter
  186. * aktprocdef renamed to current_procdef
  187. * procinfo renamed to current_procinfo
  188. * procinfo will now be stored in current_module so it can be
  189. cleaned up properly
  190. * gen_main_procsym changed to create_main_proc and release_main_proc
  191. to also generate a tprocinfo structure
  192. * fixed unit implicit initfinal
  193. Revision 1.22 2003/04/27 07:29:50 peter
  194. * current_procdef cleanup, current_procdef is now always nil when parsing
  195. a new procdef declaration
  196. * aktprocsym removed
  197. * lexlevel removed, use symtable.symtablelevel instead
  198. * implicit init/final code uses the normal genentry/genexit
  199. * funcret state checking updated for new funcret handling
  200. Revision 1.21 2002/11/15 01:58:48 peter
  201. * merged changes from 1.0.7 up to 04-11
  202. - -V option for generating bug report tracing
  203. - more tracing for option parsing
  204. - errors for cdecl and high()
  205. - win32 import stabs
  206. - win32 records<=8 are returned in eax:edx (turned off by default)
  207. - heaptrc update
  208. - more info for temp management in .s file with EXTDEBUG
  209. Revision 1.20 2002/09/09 17:34:14 peter
  210. * tdicationary.replace added to replace and item in a dictionary. This
  211. is only allowed for the same name
  212. * varsyms are inserted in symtable before the types are parsed. This
  213. fixes the long standing "var longint : longint" bug
  214. - consume_idlist and idstringlist removed. The loops are inserted
  215. at the callers place and uses the symtable for duplicate id checking
  216. Revision 1.19 2002/07/26 21:15:38 florian
  217. * rewrote the system handling
  218. Revision 1.18 2002/07/01 18:46:22 peter
  219. * internal linker
  220. * reorganized aasm layer
  221. Revision 1.17 2002/05/18 13:34:08 peter
  222. * readded missing revisions
  223. Revision 1.16 2002/05/16 19:46:37 carl
  224. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  225. + try to fix temp allocation (still in ifdef)
  226. + generic constructor calls
  227. + start of tassembler / tmodulebase class cleanup
  228. }