pexports.pas 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 : word;
  55. function IsGreater(hp1,hp2:texported_item):boolean;
  56. var
  57. i2 : boolean;
  58. begin
  59. i2:=(hp2.options and eo_index)<>0;
  60. if (hp1.options and eo_index)<>0 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:=0;
  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(true,false);
  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. message(parser_e_range_check_error)
  132. end
  133. else
  134. index:=Tordconstnode(pt).value.svalue
  135. else
  136. begin
  137. index:=0;
  138. consume(_INTCONST);
  139. end;
  140. options:=options or 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(true,false);
  150. if pt.nodetype=stringconstn then
  151. hpname:=strpas(tstringconstnode(pt).value_str)
  152. else
  153. consume(_CSTRING);
  154. options:=options or eo_name;
  155. pt.free;
  156. DefString:=hpname+'='+InternalProcName;
  157. end;
  158. if try_to_consume(_RESIDENT) then
  159. begin
  160. options:=options or eo_resident;
  161. DefString:=srsym.realname+'='+InternalProcName;{Resident ignored!}
  162. end;
  163. if (DefString<>'') and UseDeffileForExports then
  164. DefFile.AddExport(DefString);
  165. end;
  166. case srsym.typ of
  167. procsym:
  168. begin
  169. { if no specific name or index was given, then if }
  170. { the procedure has aliases defined export those, }
  171. { otherwise export the name as it appears in the }
  172. { export section (it doesn't make sense to export }
  173. { the generic mangled name, because the name of }
  174. { the parent unit is used in that) }
  175. if ((options and (eo_name or eo_index))=0) and
  176. (tprocdef(tprocsym(srsym).procdeflist[0]).aliasnames.count>1) then
  177. exportallprocsymnames(tprocsym(srsym),options)
  178. else
  179. begin
  180. { there's a name or an index -> export only one name }
  181. { correct? Or can you export multiple names with the }
  182. { same index? And/or should we also export the aliases }
  183. { if a name is specified? (JM) }
  184. if ((options and eo_name)=0) then
  185. { Export names are not mangled on Windows and OS/2 }
  186. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) then
  187. hpname:=orgs
  188. { Use set mangled name in case of cdecl/cppdecl/mwpascal }
  189. { and no name specified }
  190. else if (tprocdef(tprocsym(srsym).procdeflist[0]).proccalloption in [pocall_cdecl,pocall_mwpascal]) then
  191. hpname:=target_info.cprefix+tprocsym(srsym).realname
  192. else if (tprocdef(tprocsym(srsym).procdeflist[0]).proccalloption in [pocall_cppdecl]) then
  193. hpname:=target_info.cprefix+tprocdef(tprocsym(srsym).procdeflist[0]).cplusplusmangledname
  194. else
  195. hpname:=orgs;
  196. exportprocsym(srsym,hpname,index,options);
  197. end
  198. end;
  199. staticvarsym:
  200. begin
  201. if ((options and eo_name)=0) then
  202. { for "cvar" }
  203. if (vo_has_mangledname in tstaticvarsym(srsym).varoptions) then
  204. hpname:=srsym.mangledname
  205. else
  206. hpname:=orgs;
  207. exportvarsym(srsym,hpname,index,options);
  208. end;
  209. typesym:
  210. begin
  211. case ttypesym(srsym).typedef.typ of
  212. objectdef:
  213. case tobjectdef(ttypesym(srsym).typedef).objecttype of
  214. odt_objcclass:
  215. exportobjcclass(tobjectdef(ttypesym(srsym).typedef));
  216. else
  217. internalerror(2009092601);
  218. end;
  219. else
  220. internalerror(2009092602);
  221. end;
  222. end;
  223. end
  224. end
  225. else
  226. consume(_ID);
  227. until not try_to_consume(_COMMA);
  228. consume(_SEMICOLON);
  229. if not DefFile.empty then
  230. DefFile.writefile;
  231. end;
  232. end.