pexports.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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,symdef,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,
  46. InternalProcName : string;
  47. pd : tprocdef;
  48. pt : tnode;
  49. srsym : tsym;
  50. srsymtable : TSymtable;
  51. function IsGreater(hp1,hp2:texported_item):boolean;
  52. var
  53. i2 : boolean;
  54. begin
  55. i2:=(hp2.options and eo_index)<>0;
  56. if (hp1.options and eo_index)<>0 then
  57. begin
  58. if i2 then
  59. IsGreater:=hp1.index>hp2.index
  60. else
  61. IsGreater:=false;
  62. end
  63. else
  64. IsGreater:=i2;
  65. end;
  66. begin
  67. current_module.flags:=current_module.flags or uf_has_exports;
  68. DefString:='';
  69. InternalProcName:='';
  70. consume(_EXPORTS);
  71. repeat
  72. hp:=texported_item.create;
  73. if token=_ID then
  74. begin
  75. consume_sym_orgid(srsym,srsymtable,orgs);
  76. { orgpattern is still valid here }
  77. hp.sym:=srsym;
  78. InternalProcName:='';
  79. case srsym.typ of
  80. globalvarsym :
  81. InternalProcName:=tglobalvarsym(srsym).mangledname;
  82. typedconstsym :
  83. InternalProcName:=ttypedconstsym(srsym).mangledname;
  84. procsym :
  85. begin
  86. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  87. if (Tprocsym(srsym).ProcdefList.Count>1) or
  88. (po_kylixlocal in pd.procoptions) or
  89. ((tf_need_export in target_info.flags) and
  90. not(po_exports in pd.procoptions)) then
  91. Message(parser_e_illegal_symbol_exported)
  92. else
  93. InternalProcName:=pd.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,system_arm_wince,system_i386_wince]) 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,system_arm_wince,system_i386_wince] 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.