pexports.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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,globtype,tokens,verbose,constexp,
  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. orgs,
  44. DefString,
  45. InternalProcName : string;
  46. pd : tprocdef;
  47. pt : tnode;
  48. srsym : tsym;
  49. srsymtable : TSymtable;
  50. hpname : shortstring;
  51. index : longint;
  52. options : word;
  53. function IsGreater(hp1,hp2:texported_item):boolean;
  54. var
  55. i2 : boolean;
  56. begin
  57. i2:=(hp2.options and eo_index)<>0;
  58. if (hp1.options and eo_index)<>0 then
  59. begin
  60. if i2 then
  61. IsGreater:=hp1.index>hp2.index
  62. else
  63. IsGreater:=false;
  64. end
  65. else
  66. IsGreater:=i2;
  67. end;
  68. begin
  69. current_module.flags:=current_module.flags or uf_has_exports;
  70. DefString:='';
  71. InternalProcName:='';
  72. consume(_EXPORTS);
  73. repeat
  74. hpname:='';
  75. options:=0;
  76. index:=0;
  77. if token=_ID then
  78. begin
  79. consume_sym_orgid(srsym,srsymtable,orgs);
  80. { orgpattern is still valid here }
  81. InternalProcName:='';
  82. case srsym.typ of
  83. staticvarsym :
  84. InternalProcName:=tstaticvarsym(srsym).mangledname;
  85. procsym :
  86. begin
  87. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  88. if (Tprocsym(srsym).ProcdefList.Count>1) or
  89. (po_kylixlocal in pd.procoptions) or
  90. ((tf_need_export in target_info.flags) and
  91. not(po_exports in pd.procoptions)) then
  92. Message(parser_e_illegal_symbol_exported)
  93. else
  94. InternalProcName:=pd.mangledname;
  95. end;
  96. else
  97. Message(parser_e_illegal_symbol_exported)
  98. end;
  99. if InternalProcName<>'' then
  100. begin
  101. { This is wrong if the first is not
  102. an underline }
  103. if InternalProcName[1]='_' then
  104. delete(InternalProcName,1,1)
  105. else if (target_info.system in [system_i386_win32,system_i386_wdosx,system_arm_wince,system_i386_wince]) and UseDeffileForExports then
  106. begin
  107. Message(parser_e_dlltool_unit_var_problem);
  108. Message(parser_e_dlltool_unit_var_problem2);
  109. end;
  110. if length(InternalProcName)<2 then
  111. Message(parser_e_procname_to_short_for_export);
  112. DefString:=srsym.realname+'='+InternalProcName;
  113. end;
  114. if try_to_consume(_INDEX) then
  115. begin
  116. pt:=comp_expr(true);
  117. if pt.nodetype=ordconstn then
  118. if (Tordconstnode(pt).value<int64(low(index))) or
  119. (Tordconstnode(pt).value>int64(high(index))) then
  120. begin
  121. index:=0;
  122. message(parser_e_range_check_error)
  123. end
  124. else
  125. index:=Tordconstnode(pt).value.svalue
  126. else
  127. begin
  128. index:=0;
  129. consume(_INTCONST);
  130. end;
  131. options:=options or eo_index;
  132. pt.free;
  133. if target_info.system in [system_i386_win32,system_i386_wdosx,system_arm_wince,system_i386_wince] then
  134. DefString:=srsym.realname+'='+InternalProcName+' @ '+tostr(index)
  135. else
  136. DefString:=srsym.realname+'='+InternalProcName; {Index ignored!}
  137. end;
  138. if try_to_consume(_NAME) then
  139. begin
  140. pt:=comp_expr(true);
  141. if pt.nodetype=stringconstn then
  142. hpname:=strpas(tstringconstnode(pt).value_str)
  143. else
  144. begin
  145. consume(_CSTRING);
  146. end;
  147. options:=options or eo_name;
  148. pt.free;
  149. DefString:=hpname+'='+InternalProcName;
  150. end;
  151. if try_to_consume(_RESIDENT) then
  152. begin
  153. options:=options or eo_resident;
  154. DefString:=srsym.realname+'='+InternalProcName;{Resident ignored!}
  155. end;
  156. if (DefString<>'') and UseDeffileForExports then
  157. DefFile.AddExport(DefString);
  158. if srsym.typ=procsym then
  159. begin
  160. { if no specific name or index was given, then if }
  161. { the procedure has aliases defined export those, }
  162. { otherwise export the name as it appears in the }
  163. { export section (it doesn't make sense to export }
  164. { the generic mangled name, because the name of }
  165. { the parent unit is used in that) }
  166. if ((options and (eo_name or eo_index))=0) and
  167. (tprocdef(tprocsym(srsym).procdeflist[0]).aliasnames.count>1) then
  168. exportallprocsymnames(tprocsym(srsym),options)
  169. else
  170. begin
  171. { there's a name or an index -> export only one name }
  172. { correct? Or can you export multiple names with the }
  173. { same index? And/or should we also export the aliases }
  174. { if a name is specified? (JM) }
  175. if ((options and eo_name)=0) then
  176. { Export names are not mangled on Windows and OS/2 }
  177. if (target_info.system in (system_all_windows+[system_i386_emx, system_i386_os2])) then
  178. hpname:=orgs
  179. { Use set mangled name in case of cdecl/cppdecl/mwpascal }
  180. { and no name specified }
  181. else if (tprocdef(tprocsym(srsym).procdeflist[0]).proccalloption in [pocall_cdecl,pocall_mwpascal]) then
  182. hpname:=target_info.cprefix+tprocsym(srsym).realname
  183. else if (tprocdef(tprocsym(srsym).procdeflist[0]).proccalloption in [pocall_cppdecl]) then
  184. hpname:=target_info.cprefix+tprocdef(tprocsym(srsym).procdeflist[0]).cplusplusmangledname
  185. else
  186. hpname:=orgs;
  187. exportprocsym(srsym,hpname,index,options);
  188. end
  189. end
  190. { can also be errorsym }
  191. else if (srsym.typ=staticvarsym) then
  192. begin
  193. if ((options and eo_name)=0) then
  194. { for "cvar" }
  195. if (vo_has_mangledname in tstaticvarsym(srsym).varoptions) then
  196. hpname:=srsym.mangledname
  197. else
  198. hpname:=orgs;
  199. exportvarsym(srsym,hpname,index,options);
  200. end;
  201. end
  202. else
  203. consume(_ID);
  204. until not try_to_consume(_COMMA);
  205. consume(_SEMICOLON);
  206. if not DefFile.empty then
  207. DefFile.writefile;
  208. end;
  209. end.