pexports.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. consume_sym_orgid(srsym,srsymtable,orgs);
  75. { orgpattern is still valid here }
  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. (po_kylixlocal in tprocsym(srsym).first_procdef.procoptions) or
  87. ((tf_need_export in target_info.flags) and
  88. not(po_exports in tprocsym(srsym).first_procdef.procoptions)) then
  89. Message(parser_e_illegal_symbol_exported)
  90. else
  91. InternalProcName:=tprocsym(srsym).first_procdef.mangledname;
  92. end;
  93. else
  94. Message(parser_e_illegal_symbol_exported)
  95. end;
  96. if InternalProcName<>'' then
  97. begin
  98. { This is wrong if the first is not
  99. an underline }
  100. if InternalProcName[1]='_' then
  101. delete(InternalProcName,1,1)
  102. else if (target_info.system in [system_i386_win32,system_i386_wdosx,system_arm_wince,system_i386_wince]) and UseDeffileForExports then
  103. begin
  104. Message(parser_e_dlltool_unit_var_problem);
  105. Message(parser_e_dlltool_unit_var_problem2);
  106. end;
  107. if length(InternalProcName)<2 then
  108. Message(parser_e_procname_to_short_for_export);
  109. DefString:=srsym.realname+'='+InternalProcName;
  110. end;
  111. if try_to_consume(_INDEX) then
  112. begin
  113. pt:=comp_expr(true);
  114. if pt.nodetype=ordconstn then
  115. hp.index:=tordconstnode(pt).value
  116. else
  117. begin
  118. hp.index:=0;
  119. consume(_INTCONST);
  120. end;
  121. hp.options:=hp.options or eo_index;
  122. pt.free;
  123. if target_info.system in [system_i386_win32,system_i386_wdosx,system_arm_wince,system_i386_wince] then
  124. DefString:=srsym.realname+'='+InternalProcName+' @ '+tostr(hp.index)
  125. else
  126. DefString:=srsym.realname+'='+InternalProcName; {Index ignored!}
  127. end;
  128. if try_to_consume(_NAME) then
  129. begin
  130. pt:=comp_expr(true);
  131. if pt.nodetype=stringconstn then
  132. hp.name:=stringdup(strpas(tstringconstnode(pt).value_str))
  133. else
  134. begin
  135. hp.name:=stringdup('');
  136. consume(_CSTRING);
  137. end;
  138. hp.options:=hp.options or eo_name;
  139. pt.free;
  140. DefString:=hp.name^+'='+InternalProcName;
  141. end;
  142. if try_to_consume(_RESIDENT) then
  143. begin
  144. hp.options:=hp.options or eo_resident;
  145. DefString:=srsym.realname+'='+InternalProcName;{Resident ignored!}
  146. end;
  147. if (DefString<>'') and UseDeffileForExports then
  148. DefFile.AddExport(DefString);
  149. { Default to generate a name entry with the provided name }
  150. if not assigned(hp.name) then
  151. begin
  152. hp.name:=stringdup(orgs);
  153. hp.options:=hp.options or eo_name;
  154. end;
  155. if hp.sym.typ=procsym then
  156. exportlib.exportprocedure(hp)
  157. else
  158. exportlib.exportvar(hp);
  159. end
  160. else
  161. consume(_ID);
  162. until not try_to_consume(_COMMA);
  163. consume(_SEMICOLON);
  164. if not DefFile.empty then
  165. DefFile.writefile;
  166. end;
  167. end.