pexports.pas 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit handles the exports parsing
  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. }
  18. unit pexports;
  19. {$i fpcdefs.inc}
  20. interface
  21. { reads an exports statement in a library }
  22. procedure read_exports;
  23. implementation
  24. uses
  25. { common }
  26. cutils,
  27. { global }
  28. globals,tokens,verbose,
  29. systems,
  30. { symtable }
  31. symconst,symbase,symtype,symsym,
  32. { pass 1 }
  33. node,
  34. ncon,
  35. { parser }
  36. scanner,
  37. pbase,pexpr,
  38. { link }
  39. gendef,export
  40. {$ifdef Delphi}
  41. ,dmisc
  42. ,sysutils
  43. {$endif}
  44. ;
  45. procedure read_exports;
  46. var
  47. hp : texported_item;
  48. orgs,
  49. DefString : string;
  50. InternalProcName : string;
  51. pt : tnode;
  52. srsym : tsym;
  53. srsymtable : tsymtable;
  54. function IsGreater(hp1,hp2:texported_item):boolean;
  55. var
  56. i2 : boolean;
  57. begin
  58. i2:=(hp2.options and eo_index)<>0;
  59. if (hp1.options and eo_index)<>0 then
  60. begin
  61. if i2 then
  62. IsGreater:=hp1.index>hp2.index
  63. else
  64. IsGreater:=false;
  65. end
  66. else
  67. IsGreater:=i2;
  68. end;
  69. begin
  70. DefString:='';
  71. InternalProcName:='';
  72. consume(_EXPORTS);
  73. repeat
  74. hp:=texported_item.create;
  75. if token=_ID then
  76. begin
  77. orgs:=orgpattern;
  78. consume_sym(srsym,srsymtable);
  79. hp.sym:=srsym;
  80. InternalProcName:='';
  81. case srsym.typ of
  82. varsym :
  83. InternalProcName:=tvarsym(srsym).mangledname;
  84. typedconstsym :
  85. InternalProcName:=ttypedconstsym(srsym).mangledname;
  86. procsym :
  87. begin
  88. if (Tprocsym(srsym).procdef_count>1) or
  89. ((tf_need_export in target_info.flags) and
  90. not(po_exports in tprocsym(srsym).first_procdef.procoptions)) then
  91. Message(parser_e_illegal_symbol_exported)
  92. else
  93. InternalProcName:=tprocsym(srsym).first_procdef.mangledname;
  94. end;
  95. else
  96. Message(parser_e_illegal_symbol_exported)
  97. end;
  98. if InternalProcName<>'' then
  99. begin
  100. { This is wrong if the first is not
  101. an underline }
  102. if InternalProcName[1]='_' then
  103. delete(InternalProcName,1,1)
  104. else if (target_info.system in [system_i386_win32,system_i386_wdosx]) and UseDeffileForExports then
  105. begin
  106. Message(parser_e_dlltool_unit_var_problem);
  107. Message(parser_e_dlltool_unit_var_problem2);
  108. end;
  109. if length(InternalProcName)<2 then
  110. Message(parser_e_procname_to_short_for_export);
  111. DefString:=srsym.realname+'='+InternalProcName;
  112. end;
  113. if try_to_consume(_INDEX) then
  114. begin
  115. pt:=comp_expr(true);
  116. if pt.nodetype=ordconstn then
  117. hp.index:=tordconstnode(pt).value
  118. else
  119. begin
  120. hp.index:=0;
  121. consume(_INTCONST);
  122. end;
  123. hp.options:=hp.options or eo_index;
  124. pt.free;
  125. if target_info.system in [system_i386_win32,system_i386_wdosx] then
  126. DefString:=srsym.realname+'='+InternalProcName+' @ '+tostr(hp.index)
  127. else
  128. DefString:=srsym.realname+'='+InternalProcName; {Index ignored!}
  129. end;
  130. if try_to_consume(_NAME) then
  131. begin
  132. pt:=comp_expr(true);
  133. if pt.nodetype=stringconstn then
  134. hp.name:=stringdup(strpas(tstringconstnode(pt).value_str))
  135. else
  136. begin
  137. hp.name:=stringdup('');
  138. consume(_CSTRING);
  139. end;
  140. hp.options:=hp.options or eo_name;
  141. pt.free;
  142. DefString:=hp.name^+'='+InternalProcName;
  143. end;
  144. if try_to_consume(_RESIDENT) then
  145. begin
  146. hp.options:=hp.options or eo_resident;
  147. DefString:=srsym.realname+'='+InternalProcName;{Resident ignored!}
  148. end;
  149. if (DefString<>'') and UseDeffileForExports then
  150. DefFile.AddExport(DefString);
  151. { Default to generate a name entry with the provided name }
  152. if not assigned(hp.name) then
  153. begin
  154. hp.name:=stringdup(orgs);
  155. hp.options:=hp.options or eo_name;
  156. end;
  157. if hp.sym.typ=procsym then
  158. exportlib.exportprocedure(hp)
  159. else
  160. exportlib.exportvar(hp);
  161. end
  162. else
  163. consume(_ID);
  164. until not try_to_consume(_COMMA);
  165. consume(_SEMICOLON);
  166. if not DefFile.empty then
  167. DefFile.writefile;
  168. end;
  169. end.
  170. {
  171. $Log$
  172. Revision 1.28 2004-06-20 08:55:30 florian
  173. * logs truncated
  174. Revision 1.27 2004/06/16 20:07:09 florian
  175. * dwarf branch merged
  176. Revision 1.26 2004/04/24 17:32:05 peter
  177. index number generation for mixed index-nonindexed fixed, patch by Pavel V. Ozerski
  178. Revision 1.25.2.1 2004/05/03 14:59:57 peter
  179. * no dlltool needed for win32 linking executables
  180. Revision 1.25 2004/04/08 11:07:05 michael
  181. indexed exports needs to be sorted (patch from Pavel)
  182. }