pexports.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. current_module.flags:=current_module.flags or uf_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. consume(_INTCONST);
  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. consume(_CSTRING);
  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 (DefString<>'') and UseDeffileForExports then
  166. DefFile.AddExport(DefString);
  167. end;
  168. case srsym.typ of
  169. procsym:
  170. begin
  171. { if no specific name or index was given, then if }
  172. { the procedure has aliases defined export those, }
  173. { otherwise export the name as it appears in the }
  174. { export section (it doesn't make sense to export }
  175. { the generic mangled name, because the name of }
  176. { the parent unit is used in that) }
  177. if (options*[eo_name,eo_index]=[]) and
  178. (tprocdef(tprocsym(srsym).procdeflist[0]).aliasnames.count>1) then
  179. exportallprocsymnames(tprocsym(srsym),options)
  180. else
  181. begin
  182. { there's a name or an index -> export only one name }
  183. { correct? Or can you export multiple names with the }
  184. { same index? And/or should we also export the aliases }
  185. { if a name is specified? (JM) }
  186. if not (eo_name in options) then
  187. { Export names are not mangled on Windows and OS/2 }
  188. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) then
  189. hpname:=orgs
  190. { Use set mangled name in case of cdecl/cppdecl/mwpascal }
  191. { and no name specified }
  192. else if (tprocdef(tprocsym(srsym).procdeflist[0]).proccalloption in [pocall_cdecl,pocall_mwpascal]) then
  193. hpname:=target_info.cprefix+tprocsym(srsym).realname
  194. else if (tprocdef(tprocsym(srsym).procdeflist[0]).proccalloption in [pocall_cppdecl]) then
  195. hpname:=target_info.cprefix+tprocdef(tprocsym(srsym).procdeflist[0]).cplusplusmangledname
  196. else
  197. hpname:=orgs;
  198. exportprocsym(srsym,hpname,index,options);
  199. end
  200. end;
  201. staticvarsym:
  202. begin
  203. if not (eo_name in options) then
  204. { for "cvar" }
  205. if (vo_has_mangledname in tstaticvarsym(srsym).varoptions) then
  206. hpname:=srsym.mangledname
  207. else
  208. hpname:=orgs;
  209. exportvarsym(srsym,hpname,index,options);
  210. end;
  211. typesym:
  212. begin
  213. case ttypesym(srsym).typedef.typ of
  214. objectdef:
  215. case tobjectdef(ttypesym(srsym).typedef).objecttype of
  216. odt_objcclass:
  217. exportobjcclass(tobjectdef(ttypesym(srsym).typedef));
  218. else
  219. internalerror(2009092601);
  220. end;
  221. else
  222. internalerror(2009092602);
  223. end;
  224. end;
  225. end
  226. end
  227. else
  228. consume(_ID);
  229. until not try_to_consume(_COMMA);
  230. consume(_SEMICOLON);
  231. if not DefFile.empty then
  232. DefFile.writefile;
  233. end;
  234. end.