pexports.pas 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit handles the exports parsing
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pexports;
  19. {$i fpcdefs.inc}
  20. interface
  21. { reads an exports statement in a library }
  22. procedure read_exports;
  23. implementation
  24. uses
  25. { common }
  26. cutils,
  27. { global }
  28. globals,tokens,verbose,
  29. systems,
  30. { symtable }
  31. symconst,symbase,symtype,symsym,
  32. { pass 1 }
  33. node,
  34. ncon,
  35. { parser }
  36. scanner,
  37. pbase,pexpr,
  38. { link }
  39. gendef,export
  40. {$ifdef Delphi}
  41. ,dmisc
  42. ,sysutils
  43. {$endif}
  44. ;
  45. procedure read_exports;
  46. type
  47. pItems=^tItems;
  48. tItems=record
  49. next : pItems;
  50. item : texported_item;
  51. end;
  52. var
  53. Items, TempItems, TempItems2 : pItems;
  54. with_indexes : boolean;
  55. hp : texported_item;
  56. orgs,
  57. DefString : string;
  58. InternalProcName : string;
  59. pt : tnode;
  60. srsym : tsym;
  61. srsymtable : tsymtable;
  62. function IsGreater(hp1,hp2:texported_item):boolean;
  63. var
  64. i2 : boolean;
  65. begin
  66. i2:=(hp2.options and eo_index)<>0;
  67. if (hp1.options and eo_index)<>0 then
  68. begin
  69. if i2 then
  70. IsGreater:=hp1.index>hp2.index
  71. else
  72. IsGreater:=false;
  73. end
  74. else
  75. IsGreater:=i2;
  76. end;
  77. begin
  78. DefString:='';
  79. InternalProcName:='';
  80. consume(_EXPORTS);
  81. Items:=nil;
  82. with_indexes:=false;
  83. repeat
  84. hp:=texported_item.create;
  85. if token=_ID then
  86. begin
  87. orgs:=orgpattern;
  88. consume_sym(srsym,srsymtable);
  89. hp.sym:=srsym;
  90. InternalProcName:='';
  91. case srsym.typ of
  92. varsym :
  93. InternalProcName:=tvarsym(srsym).mangledname;
  94. typedconstsym :
  95. InternalProcName:=ttypedconstsym(srsym).mangledname;
  96. procsym :
  97. begin
  98. if (Tprocsym(srsym).procdef_count>1) or
  99. ((tf_need_export in target_info.flags) and
  100. not(po_exports in tprocsym(srsym).first_procdef.procoptions)) then
  101. Message(parser_e_illegal_symbol_exported)
  102. else
  103. InternalProcName:=tprocsym(srsym).first_procdef.mangledname;
  104. end;
  105. else
  106. Message(parser_e_illegal_symbol_exported)
  107. end;
  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]) and UseDeffileForExport 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);
  126. if pt.nodetype=ordconstn then
  127. hp.index:=tordconstnode(pt).value
  128. else
  129. begin
  130. hp.index:=0;
  131. consume(_INTCONST);
  132. end;
  133. hp.options:=hp.options or eo_index;
  134. with_indexes:=true;
  135. pt.free;
  136. if target_info.system in [system_i386_win32,system_i386_wdosx] then
  137. DefString:=srsym.realname+'='+InternalProcName+' @ '+tostr(hp.index)
  138. else
  139. DefString:=srsym.realname+'='+InternalProcName; {Index ignored!}
  140. end;
  141. if try_to_consume(_NAME) then
  142. begin
  143. pt:=comp_expr(true);
  144. if pt.nodetype=stringconstn then
  145. hp.name:=stringdup(strpas(tstringconstnode(pt).value_str))
  146. else
  147. begin
  148. hp.name:=stringdup('');
  149. consume(_CSTRING);
  150. end;
  151. hp.options:=hp.options or eo_name;
  152. pt.free;
  153. DefString:=hp.name^+'='+InternalProcName;
  154. end;
  155. if try_to_consume(_RESIDENT) then
  156. begin
  157. hp.options:=hp.options or eo_resident;
  158. DefString:=srsym.realname+'='+InternalProcName;{Resident ignored!}
  159. end;
  160. if (DefString<>'') and UseDeffileForExport then
  161. DefFile.AddExport(DefString);
  162. { Default to generate a name entry with the provided name }
  163. if not assigned(hp.name) then
  164. begin
  165. hp.name:=stringdup(orgs);
  166. hp.options:=hp.options or eo_name;
  167. end;
  168. if with_indexes then
  169. begin
  170. new(TempItems);
  171. TempItems^.Item:=hp;
  172. TempItems^.next:=Items;
  173. Items:=TempItems;
  174. end
  175. else
  176. begin
  177. if hp.sym.typ=procsym then
  178. exportlib.exportprocedure(hp)
  179. else
  180. exportlib.exportvar(hp);
  181. end;
  182. end
  183. else
  184. consume(_ID);
  185. until not try_to_consume(_COMMA);
  186. consume(_SEMICOLON);
  187. TempItems:=Items;
  188. while TempItems<>nil do
  189. begin
  190. TempItems2:=TempItems^.next;
  191. while TempItems2<>nil do
  192. begin
  193. if IsGreater(TempItems^.Item,TempItems2^.Item)then
  194. begin
  195. hp:=TempItems^.Item;
  196. TempItems^.Item:=TempItems2^.Item;
  197. TempItems2^.Item:=hp;
  198. end;
  199. TempItems2:=TempItems2^.next;
  200. end;
  201. TempItems:=TempItems^.next;
  202. end;
  203. while Items<>nil do
  204. begin
  205. if hp.sym.typ=procsym then
  206. exportlib.exportprocedure(Items^.item)
  207. else
  208. exportlib.exportvar(Items^.item);
  209. TempItems:=Items;
  210. Items:=Items^.next;
  211. Dispose(TempItems);
  212. end;
  213. if not DefFile.empty then
  214. DefFile.writefile;
  215. end;
  216. end.
  217. {
  218. $Log$
  219. Revision 1.25 2004-04-08 11:07:05 michael
  220. indexed exports needs to be sorted (patch from Pavel)
  221. Revision 1.24 2002/10/05 12:43:26 carl
  222. * fixes for Delphi 6 compilation
  223. (warning : Some features do not work under Delphi)
  224. Revision 1.23 2002/09/03 16:26:27 daniel
  225. * Make Tprocdef.defs protected
  226. Revision 1.22 2002/07/26 21:15:41 florian
  227. * rewrote the system handling
  228. Revision 1.21 2002/05/18 13:34:12 peter
  229. * readded missing revisions
  230. Revision 1.20 2002/05/16 19:46:43 carl
  231. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  232. + try to fix temp allocation (still in ifdef)
  233. + generic constructor calls
  234. + start of tassembler / tmodulebase class cleanup
  235. Revision 1.18 2002/04/04 19:06:03 peter
  236. * removed unused units
  237. * use tlocation.size in cg.a_*loc*() routines
  238. Revision 1.17 2002/04/04 18:41:07 carl
  239. + added wdosx support (patch from Pavel)
  240. }