func2subr.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #!/usr/bin/env python3
  2. """
  3. Rules for building C/API module with f2py2e.
  4. Copyright 1999,2000 Pearu Peterson all rights reserved,
  5. Pearu Peterson <pearu@ioc.ee>
  6. Permission to use, modify, and distribute this software is given under the
  7. terms of the NumPy License.
  8. NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  9. $Date: 2004/11/26 11:13:06 $
  10. Pearu Peterson
  11. """
  12. __version__ = "$Revision: 1.16 $"[10:-1]
  13. f2py_version = 'See `f2py -v`'
  14. import copy
  15. from .auxfuncs import (
  16. getfortranname, isexternal, isfunction, isfunction_wrap, isintent_in,
  17. isintent_out, islogicalfunction, ismoduleroutine, isscalar,
  18. issubroutine, issubroutine_wrap, outmess, show
  19. )
  20. def var2fixfortran(vars, a, fa=None, f90mode=None):
  21. if fa is None:
  22. fa = a
  23. if a not in vars:
  24. show(vars)
  25. outmess('var2fixfortran: No definition for argument "%s".\n' % a)
  26. return ''
  27. if 'typespec' not in vars[a]:
  28. show(vars[a])
  29. outmess('var2fixfortran: No typespec for argument "%s".\n' % a)
  30. return ''
  31. vardef = vars[a]['typespec']
  32. if vardef == 'type' and 'typename' in vars[a]:
  33. vardef = '%s(%s)' % (vardef, vars[a]['typename'])
  34. selector = {}
  35. lk = ''
  36. if 'kindselector' in vars[a]:
  37. selector = vars[a]['kindselector']
  38. lk = 'kind'
  39. elif 'charselector' in vars[a]:
  40. selector = vars[a]['charselector']
  41. lk = 'len'
  42. if '*' in selector:
  43. if f90mode:
  44. if selector['*'] in ['*', ':', '(*)']:
  45. vardef = '%s(len=*)' % (vardef)
  46. else:
  47. vardef = '%s(%s=%s)' % (vardef, lk, selector['*'])
  48. else:
  49. if selector['*'] in ['*', ':']:
  50. vardef = '%s*(%s)' % (vardef, selector['*'])
  51. else:
  52. vardef = '%s*%s' % (vardef, selector['*'])
  53. else:
  54. if 'len' in selector:
  55. vardef = '%s(len=%s' % (vardef, selector['len'])
  56. if 'kind' in selector:
  57. vardef = '%s,kind=%s)' % (vardef, selector['kind'])
  58. else:
  59. vardef = '%s)' % (vardef)
  60. elif 'kind' in selector:
  61. vardef = '%s(kind=%s)' % (vardef, selector['kind'])
  62. vardef = '%s %s' % (vardef, fa)
  63. if 'dimension' in vars[a]:
  64. vardef = '%s(%s)' % (vardef, ','.join(vars[a]['dimension']))
  65. return vardef
  66. def createfuncwrapper(rout, signature=0):
  67. assert isfunction(rout)
  68. extra_args = []
  69. vars = rout['vars']
  70. for a in rout['args']:
  71. v = rout['vars'][a]
  72. for i, d in enumerate(v.get('dimension', [])):
  73. if d == ':':
  74. dn = 'f2py_%s_d%s' % (a, i)
  75. dv = dict(typespec='integer', intent=['hide'])
  76. dv['='] = 'shape(%s, %s)' % (a, i)
  77. extra_args.append(dn)
  78. vars[dn] = dv
  79. v['dimension'][i] = dn
  80. rout['args'].extend(extra_args)
  81. need_interface = bool(extra_args)
  82. ret = ['']
  83. def add(line, ret=ret):
  84. ret[0] = '%s\n %s' % (ret[0], line)
  85. name = rout['name']
  86. fortranname = getfortranname(rout)
  87. f90mode = ismoduleroutine(rout)
  88. newname = '%sf2pywrap' % (name)
  89. if newname not in vars:
  90. vars[newname] = vars[name]
  91. args = [newname] + rout['args'][1:]
  92. else:
  93. args = [newname] + rout['args']
  94. l = var2fixfortran(vars, name, newname, f90mode)
  95. if l[:13] == 'character*(*)':
  96. if f90mode:
  97. l = 'character(len=10)' + l[13:]
  98. else:
  99. l = 'character*10' + l[13:]
  100. charselect = vars[name]['charselector']
  101. if charselect.get('*', '') == '(*)':
  102. charselect['*'] = '10'
  103. sargs = ', '.join(args)
  104. if f90mode:
  105. add('subroutine f2pywrap_%s_%s (%s)' %
  106. (rout['modulename'], name, sargs))
  107. if not signature:
  108. add('use %s, only : %s' % (rout['modulename'], fortranname))
  109. else:
  110. add('subroutine f2pywrap%s (%s)' % (name, sargs))
  111. if not need_interface:
  112. add('external %s' % (fortranname))
  113. l = l + ', ' + fortranname
  114. if need_interface:
  115. for line in rout['saved_interface'].split('\n'):
  116. if line.lstrip().startswith('use ') and '__user__' not in line:
  117. add(line)
  118. args = args[1:]
  119. dumped_args = []
  120. for a in args:
  121. if isexternal(vars[a]):
  122. add('external %s' % (a))
  123. dumped_args.append(a)
  124. for a in args:
  125. if a in dumped_args:
  126. continue
  127. if isscalar(vars[a]):
  128. add(var2fixfortran(vars, a, f90mode=f90mode))
  129. dumped_args.append(a)
  130. for a in args:
  131. if a in dumped_args:
  132. continue
  133. if isintent_in(vars[a]):
  134. add(var2fixfortran(vars, a, f90mode=f90mode))
  135. dumped_args.append(a)
  136. for a in args:
  137. if a in dumped_args:
  138. continue
  139. add(var2fixfortran(vars, a, f90mode=f90mode))
  140. add(l)
  141. if need_interface:
  142. if f90mode:
  143. # f90 module already defines needed interface
  144. pass
  145. else:
  146. add('interface')
  147. add(rout['saved_interface'].lstrip())
  148. add('end interface')
  149. sargs = ', '.join([a for a in args if a not in extra_args])
  150. if not signature:
  151. if islogicalfunction(rout):
  152. add('%s = .not.(.not.%s(%s))' % (newname, fortranname, sargs))
  153. else:
  154. add('%s = %s(%s)' % (newname, fortranname, sargs))
  155. if f90mode:
  156. add('end subroutine f2pywrap_%s_%s' % (rout['modulename'], name))
  157. else:
  158. add('end')
  159. return ret[0]
  160. def createsubrwrapper(rout, signature=0):
  161. assert issubroutine(rout)
  162. extra_args = []
  163. vars = rout['vars']
  164. for a in rout['args']:
  165. v = rout['vars'][a]
  166. for i, d in enumerate(v.get('dimension', [])):
  167. if d == ':':
  168. dn = 'f2py_%s_d%s' % (a, i)
  169. dv = dict(typespec='integer', intent=['hide'])
  170. dv['='] = 'shape(%s, %s)' % (a, i)
  171. extra_args.append(dn)
  172. vars[dn] = dv
  173. v['dimension'][i] = dn
  174. rout['args'].extend(extra_args)
  175. need_interface = bool(extra_args)
  176. ret = ['']
  177. def add(line, ret=ret):
  178. ret[0] = '%s\n %s' % (ret[0], line)
  179. name = rout['name']
  180. fortranname = getfortranname(rout)
  181. f90mode = ismoduleroutine(rout)
  182. args = rout['args']
  183. sargs = ', '.join(args)
  184. if f90mode:
  185. add('subroutine f2pywrap_%s_%s (%s)' %
  186. (rout['modulename'], name, sargs))
  187. if not signature:
  188. add('use %s, only : %s' % (rout['modulename'], fortranname))
  189. else:
  190. add('subroutine f2pywrap%s (%s)' % (name, sargs))
  191. if not need_interface:
  192. add('external %s' % (fortranname))
  193. if need_interface:
  194. for line in rout['saved_interface'].split('\n'):
  195. if line.lstrip().startswith('use ') and '__user__' not in line:
  196. add(line)
  197. dumped_args = []
  198. for a in args:
  199. if isexternal(vars[a]):
  200. add('external %s' % (a))
  201. dumped_args.append(a)
  202. for a in args:
  203. if a in dumped_args:
  204. continue
  205. if isscalar(vars[a]):
  206. add(var2fixfortran(vars, a, f90mode=f90mode))
  207. dumped_args.append(a)
  208. for a in args:
  209. if a in dumped_args:
  210. continue
  211. add(var2fixfortran(vars, a, f90mode=f90mode))
  212. if need_interface:
  213. if f90mode:
  214. # f90 module already defines needed interface
  215. pass
  216. else:
  217. add('interface')
  218. for line in rout['saved_interface'].split('\n'):
  219. if line.lstrip().startswith('use ') and '__user__' in line:
  220. continue
  221. add(line)
  222. add('end interface')
  223. sargs = ', '.join([a for a in args if a not in extra_args])
  224. if not signature:
  225. add('call %s(%s)' % (fortranname, sargs))
  226. if f90mode:
  227. add('end subroutine f2pywrap_%s_%s' % (rout['modulename'], name))
  228. else:
  229. add('end')
  230. return ret[0]
  231. def assubr(rout):
  232. if isfunction_wrap(rout):
  233. fortranname = getfortranname(rout)
  234. name = rout['name']
  235. outmess('\t\tCreating wrapper for Fortran function "%s"("%s")...\n' % (
  236. name, fortranname))
  237. rout = copy.copy(rout)
  238. fname = name
  239. rname = fname
  240. if 'result' in rout:
  241. rname = rout['result']
  242. rout['vars'][fname] = rout['vars'][rname]
  243. fvar = rout['vars'][fname]
  244. if not isintent_out(fvar):
  245. if 'intent' not in fvar:
  246. fvar['intent'] = []
  247. fvar['intent'].append('out')
  248. flag = 1
  249. for i in fvar['intent']:
  250. if i.startswith('out='):
  251. flag = 0
  252. break
  253. if flag:
  254. fvar['intent'].append('out=%s' % (rname))
  255. rout['args'][:] = [fname] + rout['args']
  256. return rout, createfuncwrapper(rout)
  257. if issubroutine_wrap(rout):
  258. fortranname = getfortranname(rout)
  259. name = rout['name']
  260. outmess('\t\tCreating wrapper for Fortran subroutine "%s"("%s")...\n' % (
  261. name, fortranname))
  262. rout = copy.copy(rout)
  263. return rout, createsubrwrapper(rout)
  264. return rout, ''