pexports.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. { obj-c }
  39. objcutil,
  40. { link }
  41. gendef,export
  42. ;
  43. procedure read_exports;
  44. var
  45. orgs,
  46. DefString,
  47. InternalProcName : string;
  48. pd : tprocdef;
  49. pt : tnode;
  50. srsym : tsym;
  51. srsymtable : TSymtable;
  52. hpname : shortstring;
  53. index : longint;
  54. options : texportoptions;
  55. function IsGreater(hp1,hp2:texported_item):boolean;
  56. var
  57. i2 : boolean;
  58. begin
  59. i2:=eo_index in hp2.options;
  60. if eo_index in hp1.options then
  61. begin
  62. if i2 then
  63. IsGreater:=hp1.index>hp2.index
  64. else
  65. IsGreater:=false;
  66. end
  67. else
  68. IsGreater:=i2;
  69. end;
  70. begin
  71. include(current_module.moduleflags,mf_has_exports);
  72. DefString:='';
  73. InternalProcName:='';
  74. consume(_EXPORTS);
  75. repeat
  76. hpname:='';
  77. options:=[];
  78. index:=0;
  79. if token=_ID then
  80. begin
  81. consume_sym_orgid(srsym,srsymtable,orgs);
  82. { orgpattern is still valid here }
  83. InternalProcName:='';
  84. case srsym.typ of
  85. staticvarsym :
  86. InternalProcName:=tstaticvarsym(srsym).mangledname;
  87. procsym :
  88. begin
  89. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  90. if (Tprocsym(srsym).ProcdefList.Count>1) or
  91. (po_kylixlocal in pd.procoptions) or
  92. ((tf_need_export in target_info.flags) and
  93. not(po_exports in pd.procoptions)) then
  94. Message(parser_e_illegal_symbol_exported)
  95. else
  96. InternalProcName:=pd.mangledname;
  97. end;
  98. typesym :
  99. begin
  100. if not is_objcclass(ttypesym(srsym).typedef) then
  101. Message(parser_e_illegal_symbol_exported)
  102. end;
  103. else
  104. Message(parser_e_illegal_symbol_exported)
  105. end;
  106. if (srsym.typ<>typesym) then
  107. begin
  108. if InternalProcName<>'' then
  109. begin
  110. { This is wrong if the first is not
  111. an underline }
  112. if InternalProcName[1]='_' then
  113. delete(InternalProcName,1,1)
  114. else if (target_info.system in [system_i386_win32,system_i386_wdosx,system_arm_wince,system_i386_wince]) and UseDeffileForExports then
  115. begin
  116. Message(parser_e_dlltool_unit_var_problem);
  117. Message(parser_e_dlltool_unit_var_problem2);
  118. end;
  119. if length(InternalProcName)<2 then
  120. Message(parser_e_procname_to_short_for_export);
  121. DefString:=srsym.realname+'='+InternalProcName;
  122. end;
  123. if try_to_consume(_INDEX) then
  124. begin
  125. pt:=comp_expr([ef_accept_equal]);
  126. if pt.nodetype=ordconstn then
  127. if (Tordconstnode(pt).value<int64(low(index))) or
  128. (Tordconstnode(pt).value>int64(high(index))) then
  129. begin
  130. index:=0;
  131. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(index)),tostr(high(index)))
  132. end
  133. else
  134. index:=Tordconstnode(pt).value.svalue
  135. else
  136. begin
  137. index:=0;
  138. message(type_e_ordinal_expr_expected);
  139. end;
  140. include(options,eo_index);
  141. pt.free;
  142. if target_info.system in [system_i386_win32,system_i386_wdosx,system_arm_wince,system_i386_wince] then
  143. DefString:=srsym.realname+'='+InternalProcName+' @ '+tostr(index)
  144. else
  145. DefString:=srsym.realname+'='+InternalProcName; {Index ignored!}
  146. end;
  147. if try_to_consume(_NAME) then
  148. begin
  149. pt:=comp_expr([ef_accept_equal]);
  150. if pt.nodetype=stringconstn then
  151. hpname:=strpas(tstringconstnode(pt).value_str)
  152. else if is_constcharnode(pt) then
  153. hpname:=chr(tordconstnode(pt).value.svalue and $ff)
  154. else
  155. message(type_e_string_expr_expected);
  156. include(options,eo_name);
  157. pt.free;
  158. DefString:=hpname+'='+InternalProcName;
  159. end;
  160. if try_to_consume(_RESIDENT) then
  161. begin
  162. include(options,eo_resident);
  163. DefString:=srsym.realname+'='+InternalProcName;{Resident ignored!}
  164. end;
  165. if try_to_consume(_PROMISING) then
  166. begin
  167. if target_info.system in systems_wasm then
  168. begin
  169. if try_to_consume(_FIRST) then
  170. include(options,eo_promising_first)
  171. else if try_to_consume(_LAST) then
  172. include(options,eo_promising_last)
  173. else
  174. include(options,eo_promising_first);
  175. end
  176. else
  177. begin
  178. Message(parser_e_promising_exports_not_supported_on_current_platform);
  179. if not try_to_consume(_FIRST) then
  180. try_to_consume(_LAST);
  181. end;
  182. end;
  183. if (DefString<>'') and UseDeffileForExports then
  184. DefFile.AddExport(DefString);
  185. end;
  186. case srsym.typ of
  187. procsym:
  188. begin
  189. { if no specific name or index was given, then if }
  190. { the procedure has aliases defined export those, }
  191. { otherwise export the name as it appears in the }
  192. { export section (it doesn't make sense to export }
  193. { the generic mangled name, because the name of }
  194. { the parent unit is used in that) }
  195. if (options*[eo_name,eo_index]=[]) and
  196. (tprocdef(tprocsym(srsym).procdeflist[0]).aliasnames.count>1) then
  197. exportallprocsymnames(tprocsym(srsym),options)
  198. else
  199. begin
  200. { there's a name or an index -> export only one name }
  201. { correct? Or can you export multiple names with the }
  202. { same index? And/or should we also export the aliases }
  203. { if a name is specified? (JM) }
  204. if not (eo_name in options) then
  205. { Export names are not mangled on Windows and OS/2 }
  206. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) then
  207. hpname:=orgs
  208. { Use set mangled name in case of cdecl/cppdecl/mwpascal }
  209. { and no name specified }
  210. else if (tprocdef(tprocsym(srsym).procdeflist[0]).proccalloption in [pocall_cdecl,pocall_mwpascal]) then
  211. hpname:=target_info.cprefix+tprocsym(srsym).realname
  212. else if (tprocdef(tprocsym(srsym).procdeflist[0]).proccalloption in [pocall_cppdecl]) then
  213. hpname:=target_info.cprefix+tprocdef(tprocsym(srsym).procdeflist[0]).cplusplusmangledname
  214. else
  215. hpname:=orgs;
  216. exportprocsym(srsym,hpname,index,options);
  217. end
  218. end;
  219. staticvarsym:
  220. begin
  221. if not (eo_name in options) then
  222. { for "cvar" }
  223. if (vo_has_mangledname in tstaticvarsym(srsym).varoptions) then
  224. hpname:=srsym.mangledname
  225. else
  226. hpname:=orgs;
  227. exportvarsym(srsym,hpname,index,options);
  228. end;
  229. typesym:
  230. begin
  231. case ttypesym(srsym).typedef.typ of
  232. objectdef:
  233. case tobjectdef(ttypesym(srsym).typedef).objecttype of
  234. odt_objcclass:
  235. exportobjcclass(tobjectdef(ttypesym(srsym).typedef));
  236. else
  237. internalerror(2009092601);
  238. end;
  239. else
  240. internalerror(2009092602);
  241. end;
  242. end;
  243. else
  244. internalerror(2019050502);
  245. end
  246. end
  247. else
  248. consume(_ID);
  249. until not try_to_consume(_COMMA);
  250. consume(_SEMICOLON);
  251. if not DefFile.empty then
  252. DefFile.writefile;
  253. end;
  254. end.