pexports.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. This unit handles the exports parsing
  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 pexports;
  18. {$i fpcdefs.inc}
  19. interface
  20. { reads an exports statement in a library }
  21. procedure read_exports;
  22. implementation
  23. uses
  24. { common }
  25. cutils,
  26. { global }
  27. globals,tokens,verbose,
  28. systems,
  29. ppu,fmodule,
  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. ;
  41. procedure read_exports;
  42. var
  43. hp : texported_item;
  44. orgs,
  45. DefString : string;
  46. InternalProcName : string;
  47. pt : tnode;
  48. srsym : tsym;
  49. srsymtable : tsymtable;
  50. function IsGreater(hp1,hp2:texported_item):boolean;
  51. var
  52. i2 : boolean;
  53. begin
  54. i2:=(hp2.options and eo_index)<>0;
  55. if (hp1.options and eo_index)<>0 then
  56. begin
  57. if i2 then
  58. IsGreater:=hp1.index>hp2.index
  59. else
  60. IsGreater:=false;
  61. end
  62. else
  63. IsGreater:=i2;
  64. end;
  65. begin
  66. current_module.flags:=current_module.flags or uf_has_exports;
  67. DefString:='';
  68. InternalProcName:='';
  69. consume(_EXPORTS);
  70. repeat
  71. hp:=texported_item.create;
  72. if token=_ID then
  73. begin
  74. orgs:=orgpattern;
  75. consume_sym(srsym,srsymtable);
  76. hp.sym:=srsym;
  77. InternalProcName:='';
  78. case srsym.typ of
  79. globalvarsym :
  80. InternalProcName:=tglobalvarsym(srsym).mangledname;
  81. typedconstsym :
  82. InternalProcName:=ttypedconstsym(srsym).mangledname;
  83. procsym :
  84. begin
  85. if (Tprocsym(srsym).procdef_count>1) or
  86. ((tf_need_export in target_info.flags) and
  87. not(po_exports in tprocsym(srsym).first_procdef.procoptions)) then
  88. Message(parser_e_illegal_symbol_exported)
  89. else
  90. InternalProcName:=tprocsym(srsym).first_procdef.mangledname;
  91. end;
  92. else
  93. Message(parser_e_illegal_symbol_exported)
  94. end;
  95. if InternalProcName<>'' then
  96. begin
  97. { This is wrong if the first is not
  98. an underline }
  99. if InternalProcName[1]='_' then
  100. delete(InternalProcName,1,1)
  101. else if (target_info.system in [system_i386_win32,system_i386_wdosx,system_arm_wince,system_i386_wince]) and UseDeffileForExports then
  102. begin
  103. Message(parser_e_dlltool_unit_var_problem);
  104. Message(parser_e_dlltool_unit_var_problem2);
  105. end;
  106. if length(InternalProcName)<2 then
  107. Message(parser_e_procname_to_short_for_export);
  108. DefString:=srsym.realname+'='+InternalProcName;
  109. end;
  110. if try_to_consume(_INDEX) then
  111. begin
  112. pt:=comp_expr(true);
  113. if pt.nodetype=ordconstn then
  114. hp.index:=tordconstnode(pt).value
  115. else
  116. begin
  117. hp.index:=0;
  118. consume(_INTCONST);
  119. end;
  120. hp.options:=hp.options or eo_index;
  121. pt.free;
  122. if target_info.system in [system_i386_win32,system_i386_wdosx,system_arm_wince,system_i386_wince] then
  123. DefString:=srsym.realname+'='+InternalProcName+' @ '+tostr(hp.index)
  124. else
  125. DefString:=srsym.realname+'='+InternalProcName; {Index ignored!}
  126. end;
  127. if try_to_consume(_NAME) then
  128. begin
  129. pt:=comp_expr(true);
  130. if pt.nodetype=stringconstn then
  131. hp.name:=stringdup(strpas(tstringconstnode(pt).value_str))
  132. else
  133. begin
  134. hp.name:=stringdup('');
  135. consume(_CSTRING);
  136. end;
  137. hp.options:=hp.options or eo_name;
  138. pt.free;
  139. DefString:=hp.name^+'='+InternalProcName;
  140. end;
  141. if try_to_consume(_RESIDENT) then
  142. begin
  143. hp.options:=hp.options or eo_resident;
  144. DefString:=srsym.realname+'='+InternalProcName;{Resident ignored!}
  145. end;
  146. if (DefString<>'') and UseDeffileForExports then
  147. DefFile.AddExport(DefString);
  148. { Default to generate a name entry with the provided name }
  149. if not assigned(hp.name) then
  150. begin
  151. hp.name:=stringdup(orgs);
  152. hp.options:=hp.options or eo_name;
  153. end;
  154. if hp.sym.typ=procsym then
  155. exportlib.exportprocedure(hp)
  156. else
  157. exportlib.exportvar(hp);
  158. end
  159. else
  160. consume(_ID);
  161. until not try_to_consume(_COMMA);
  162. consume(_SEMICOLON);
  163. if not DefFile.empty then
  164. DefFile.writefile;
  165. end;
  166. end.