export.pas 7.0 KB

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