export.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an uniform export object
  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 export;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,cclasses,
  22. systems,
  23. symtype,symdef,symsym,
  24. aasmdata;
  25. type
  26. { export options }
  27. texportoption=(eo_none,
  28. eo_resident,
  29. eo_index,
  30. eo_name,
  31. eo_no_sym_name, { don't try to use another mangled name if symbol is known }
  32. eo_promising_first,
  33. eo_promising_last
  34. );
  35. texportoptions=set of texportoption;
  36. texported_item = class(TLinkedListItem)
  37. sym : tsym;
  38. index : longint;
  39. name : pshortstring;
  40. options : texportoptions;
  41. is_var : boolean;
  42. constructor create;
  43. destructor destroy;override;
  44. end;
  45. texportlib=class
  46. private
  47. notsupmsg : boolean;
  48. fignoreduplicates : boolean;
  49. finitname,
  50. ffininame : string;
  51. procedure NotSupported;
  52. protected
  53. procedure duplicatesymbol(const s:string);
  54. public
  55. constructor Create;virtual;
  56. destructor Destroy;override;
  57. procedure preparelib(const s : string);virtual;
  58. procedure exportprocedure(hp : texported_item);virtual;
  59. procedure exportvar(hp : texported_item);virtual;
  60. procedure generatelib;virtual;
  61. procedure setinitname(list: TAsmList; const s: string); virtual;
  62. procedure setfininame(list: TAsmList; const s: string); virtual;
  63. property initname: string read finitname;
  64. property fininame: string read ffininame;
  65. property ignoreduplicates : boolean read fignoreduplicates write fignoreduplicates;
  66. end;
  67. TExportLibClass=class of TExportLib;
  68. procedure exportprocsym(sym: tsym; const s : string; index: longint; options: texportoptions);
  69. procedure exportvarsym(sym: tsym; const s : string; index: longint; options: texportoptions);
  70. { to export symbols not directly related to a tsym (e.g., the Objective-C
  71. rtti) }
  72. procedure exportname(const s : string; options: texportoptions);
  73. procedure exportallprocdefnames(sym: tprocsym; pd: tprocdef; options: texportoptions);
  74. procedure exportallprocsymnames(ps: tprocsym; options: texportoptions);
  75. var
  76. CExportLib : array[tsystem] of TExportLibClass;
  77. ExportLib : TExportLib;
  78. procedure RegisterExport(t:tsystem;c:TExportLibClass);
  79. procedure InitExport;
  80. procedure DoneExport;
  81. implementation
  82. uses
  83. verbose,globals;
  84. {****************************************************************************
  85. TExported_procedure
  86. ****************************************************************************}
  87. procedure exportprocsym(sym: tsym; const s : string; index: longint; options: texportoptions);
  88. var
  89. hp : texported_item;
  90. begin
  91. hp:=texported_item.create;
  92. hp.name:=stringdup(s);
  93. hp.sym:=sym;
  94. hp.options:=options+[eo_name];
  95. hp.index:=index;
  96. exportlib.exportprocedure(hp);
  97. end;
  98. procedure exportvarsym(sym: tsym; const s : string; index: longint; options: texportoptions);
  99. var
  100. hp : texported_item;
  101. begin
  102. hp:=texported_item.create;
  103. hp.name:=stringdup(s);
  104. hp.sym:=sym;
  105. hp.is_var:=true;
  106. hp.options:=options+[eo_name];
  107. hp.index:=index;
  108. exportlib.exportvar(hp);
  109. end;
  110. procedure exportname(const s : string; options: texportoptions);
  111. begin
  112. exportvarsym(nil,s,0,options);
  113. end;
  114. procedure exportallprocdefnames(sym: tprocsym; pd: tprocdef; options: texportoptions);
  115. var
  116. item: TCmdStrListItem;
  117. begin
  118. exportprocsym(sym,pd.mangledname,0,options);
  119. { walk through all aliases }
  120. item:=TCmdStrListItem(pd.aliasnames.first);
  121. while assigned(item) do
  122. begin
  123. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  124. if item.str<>pd.mangledname then
  125. exportprocsym(sym,item.str,0,options);
  126. item:=TCmdStrListItem(item.next);
  127. end;
  128. end;
  129. procedure exportallprocsymnames(ps: tprocsym; options: texportoptions);
  130. var
  131. i: longint;
  132. begin
  133. for i:= 0 to ps.ProcdefList.Count-1 do
  134. exportallprocdefnames(ps,tprocdef(ps.ProcdefList[i]),options);
  135. end;
  136. {****************************************************************************
  137. TExported_procedure
  138. ****************************************************************************}
  139. constructor texported_item.Create;
  140. begin
  141. inherited Create;
  142. sym:=nil;
  143. index:=-1;
  144. name:=nil;
  145. options:=[];
  146. is_var:=false;
  147. end;
  148. destructor texported_item.destroy;
  149. begin
  150. stringdispose(name);
  151. inherited destroy;
  152. end;
  153. {****************************************************************************
  154. TExportLib
  155. ****************************************************************************}
  156. constructor texportlib.Create;
  157. begin
  158. notsupmsg:=false;
  159. fignoreduplicates:=false;
  160. end;
  161. destructor texportlib.Destroy;
  162. begin
  163. end;
  164. procedure texportlib.NotSupported;
  165. begin
  166. { show the message only once }
  167. if not notsupmsg then
  168. begin
  169. Message(exec_e_dll_not_supported);
  170. notsupmsg:=true;
  171. end;
  172. end;
  173. procedure texportlib.duplicatesymbol(const s: string);
  174. begin
  175. { only generate an error if the caller is not aware that it could generate
  176. duplicates (e.g. exporting from a package) }
  177. if not ignoreduplicates then
  178. Message1(parser_e_export_name_double,s);
  179. end;
  180. procedure texportlib.preparelib(const s:string);
  181. begin
  182. NotSupported;
  183. end;
  184. procedure texportlib.exportprocedure(hp : texported_item);
  185. begin
  186. NotSupported;
  187. end;
  188. procedure texportlib.exportvar(hp : texported_item);
  189. begin
  190. NotSupported;
  191. end;
  192. procedure texportlib.generatelib;
  193. begin
  194. NotSupported;
  195. end;
  196. procedure texportlib.setinitname(list: TAsmList; const s: string);
  197. begin
  198. finitname:=s;
  199. end;
  200. procedure texportlib.setfininame(list: TAsmList; const s: string);
  201. begin
  202. ffininame:=s;
  203. end;
  204. {*****************************************************************************
  205. Init/Done
  206. *****************************************************************************}
  207. procedure RegisterExport(t:tsystem;c:TExportLibClass);
  208. begin
  209. CExportLib[t]:=c;
  210. end;
  211. procedure InitExport;
  212. begin
  213. if assigned(CExportLib[target_info.system]) then
  214. exportlib:=CExportLib[target_info.system].Create
  215. else
  216. exportlib:=TExportLib.Create;
  217. end;
  218. procedure DoneExport;
  219. begin
  220. if assigned(Exportlib) then
  221. Exportlib.free;
  222. end;
  223. end.