export.pas 6.9 KB

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