export.pas 7.0 KB

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