Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

1544 rader
61 KiB

  1. import os, sys, io
  2. from . import ffiplatform, model
  3. from .error import VerificationError
  4. from .cffi_opcode import *
  5. VERSION_BASE = 0x2601
  6. VERSION_EMBEDDED = 0x2701
  7. VERSION_CHAR16CHAR32 = 0x2801
  8. class GlobalExpr:
  9. def __init__(self, name, address, type_op, size=0, check_value=0):
  10. self.name = name
  11. self.address = address
  12. self.type_op = type_op
  13. self.size = size
  14. self.check_value = check_value
  15. def as_c_expr(self):
  16. return ' { "%s", (void *)%s, %s, (void *)%s },' % (
  17. self.name, self.address, self.type_op.as_c_expr(), self.size)
  18. def as_python_expr(self):
  19. return "b'%s%s',%d" % (self.type_op.as_python_bytes(), self.name,
  20. self.check_value)
  21. class FieldExpr:
  22. def __init__(self, name, field_offset, field_size, fbitsize, field_type_op):
  23. self.name = name
  24. self.field_offset = field_offset
  25. self.field_size = field_size
  26. self.fbitsize = fbitsize
  27. self.field_type_op = field_type_op
  28. def as_c_expr(self):
  29. spaces = " " * len(self.name)
  30. return (' { "%s", %s,\n' % (self.name, self.field_offset) +
  31. ' %s %s,\n' % (spaces, self.field_size) +
  32. ' %s %s },' % (spaces, self.field_type_op.as_c_expr()))
  33. def as_python_expr(self):
  34. raise NotImplementedError
  35. def as_field_python_expr(self):
  36. if self.field_type_op.op == OP_NOOP:
  37. size_expr = ''
  38. elif self.field_type_op.op == OP_BITFIELD:
  39. size_expr = format_four_bytes(self.fbitsize)
  40. else:
  41. raise NotImplementedError
  42. return "b'%s%s%s'" % (self.field_type_op.as_python_bytes(),
  43. size_expr,
  44. self.name)
  45. class StructUnionExpr:
  46. def __init__(self, name, type_index, flags, size, alignment, comment,
  47. first_field_index, c_fields):
  48. self.name = name
  49. self.type_index = type_index
  50. self.flags = flags
  51. self.size = size
  52. self.alignment = alignment
  53. self.comment = comment
  54. self.first_field_index = first_field_index
  55. self.c_fields = c_fields
  56. def as_c_expr(self):
  57. return (' { "%s", %d, %s,' % (self.name, self.type_index, self.flags)
  58. + '\n %s, %s, ' % (self.size, self.alignment)
  59. + '%d, %d ' % (self.first_field_index, len(self.c_fields))
  60. + ('/* %s */ ' % self.comment if self.comment else '')
  61. + '},')
  62. def as_python_expr(self):
  63. flags = eval(self.flags, G_FLAGS)
  64. fields_expr = [c_field.as_field_python_expr()
  65. for c_field in self.c_fields]
  66. return "(b'%s%s%s',%s)" % (
  67. format_four_bytes(self.type_index),
  68. format_four_bytes(flags),
  69. self.name,
  70. ','.join(fields_expr))
  71. class EnumExpr:
  72. def __init__(self, name, type_index, size, signed, allenums):
  73. self.name = name
  74. self.type_index = type_index
  75. self.size = size
  76. self.signed = signed
  77. self.allenums = allenums
  78. def as_c_expr(self):
  79. return (' { "%s", %d, _cffi_prim_int(%s, %s),\n'
  80. ' "%s" },' % (self.name, self.type_index,
  81. self.size, self.signed, self.allenums))
  82. def as_python_expr(self):
  83. prim_index = {
  84. (1, 0): PRIM_UINT8, (1, 1): PRIM_INT8,
  85. (2, 0): PRIM_UINT16, (2, 1): PRIM_INT16,
  86. (4, 0): PRIM_UINT32, (4, 1): PRIM_INT32,
  87. (8, 0): PRIM_UINT64, (8, 1): PRIM_INT64,
  88. }[self.size, self.signed]
  89. return "b'%s%s%s\\x00%s'" % (format_four_bytes(self.type_index),
  90. format_four_bytes(prim_index),
  91. self.name, self.allenums)
  92. class TypenameExpr:
  93. def __init__(self, name, type_index):
  94. self.name = name
  95. self.type_index = type_index
  96. def as_c_expr(self):
  97. return ' { "%s", %d },' % (self.name, self.type_index)
  98. def as_python_expr(self):
  99. return "b'%s%s'" % (format_four_bytes(self.type_index), self.name)
  100. # ____________________________________________________________
  101. class Recompiler:
  102. _num_externpy = 0
  103. def __init__(self, ffi, module_name, target_is_python=False):
  104. self.ffi = ffi
  105. self.module_name = module_name
  106. self.target_is_python = target_is_python
  107. self._version = VERSION_BASE
  108. def needs_version(self, ver):
  109. self._version = max(self._version, ver)
  110. def collect_type_table(self):
  111. self._typesdict = {}
  112. self._generate("collecttype")
  113. #
  114. all_decls = sorted(self._typesdict, key=str)
  115. #
  116. # prepare all FUNCTION bytecode sequences first
  117. self.cffi_types = []
  118. for tp in all_decls:
  119. if tp.is_raw_function:
  120. assert self._typesdict[tp] is None
  121. self._typesdict[tp] = len(self.cffi_types)
  122. self.cffi_types.append(tp) # placeholder
  123. for tp1 in tp.args:
  124. assert isinstance(tp1, (model.VoidType,
  125. model.BasePrimitiveType,
  126. model.PointerType,
  127. model.StructOrUnionOrEnum,
  128. model.FunctionPtrType))
  129. if self._typesdict[tp1] is None:
  130. self._typesdict[tp1] = len(self.cffi_types)
  131. self.cffi_types.append(tp1) # placeholder
  132. self.cffi_types.append('END') # placeholder
  133. #
  134. # prepare all OTHER bytecode sequences
  135. for tp in all_decls:
  136. if not tp.is_raw_function and self._typesdict[tp] is None:
  137. self._typesdict[tp] = len(self.cffi_types)
  138. self.cffi_types.append(tp) # placeholder
  139. if tp.is_array_type and tp.length is not None:
  140. self.cffi_types.append('LEN') # placeholder
  141. assert None not in self._typesdict.values()
  142. #
  143. # collect all structs and unions and enums
  144. self._struct_unions = {}
  145. self._enums = {}
  146. for tp in all_decls:
  147. if isinstance(tp, model.StructOrUnion):
  148. self._struct_unions[tp] = None
  149. elif isinstance(tp, model.EnumType):
  150. self._enums[tp] = None
  151. for i, tp in enumerate(sorted(self._struct_unions,
  152. key=lambda tp: tp.name)):
  153. self._struct_unions[tp] = i
  154. for i, tp in enumerate(sorted(self._enums,
  155. key=lambda tp: tp.name)):
  156. self._enums[tp] = i
  157. #
  158. # emit all bytecode sequences now
  159. for tp in all_decls:
  160. method = getattr(self, '_emit_bytecode_' + tp.__class__.__name__)
  161. method(tp, self._typesdict[tp])
  162. #
  163. # consistency check
  164. for op in self.cffi_types:
  165. assert isinstance(op, CffiOp)
  166. self.cffi_types = tuple(self.cffi_types) # don't change any more
  167. def _do_collect_type(self, tp):
  168. if not isinstance(tp, model.BaseTypeByIdentity):
  169. if isinstance(tp, tuple):
  170. for x in tp:
  171. self._do_collect_type(x)
  172. return
  173. if tp not in self._typesdict:
  174. self._typesdict[tp] = None
  175. if isinstance(tp, model.FunctionPtrType):
  176. self._do_collect_type(tp.as_raw_function())
  177. elif isinstance(tp, model.StructOrUnion):
  178. if tp.fldtypes is not None and (
  179. tp not in self.ffi._parser._included_declarations):
  180. for name1, tp1, _, _ in tp.enumfields():
  181. self._do_collect_type(self._field_type(tp, name1, tp1))
  182. else:
  183. for _, x in tp._get_items():
  184. self._do_collect_type(x)
  185. def _generate(self, step_name):
  186. lst = self.ffi._parser._declarations.items()
  187. for name, (tp, quals) in sorted(lst):
  188. kind, realname = name.split(' ', 1)
  189. try:
  190. method = getattr(self, '_generate_cpy_%s_%s' % (kind,
  191. step_name))
  192. except AttributeError:
  193. raise VerificationError(
  194. "not implemented in recompile(): %r" % name)
  195. try:
  196. self._current_quals = quals
  197. method(tp, realname)
  198. except Exception as e:
  199. model.attach_exception_info(e, name)
  200. raise
  201. # ----------
  202. ALL_STEPS = ["global", "field", "struct_union", "enum", "typename"]
  203. def collect_step_tables(self):
  204. # collect the declarations for '_cffi_globals', '_cffi_typenames', etc.
  205. self._lsts = {}
  206. for step_name in self.ALL_STEPS:
  207. self._lsts[step_name] = []
  208. self._seen_struct_unions = set()
  209. self._generate("ctx")
  210. self._add_missing_struct_unions()
  211. #
  212. for step_name in self.ALL_STEPS:
  213. lst = self._lsts[step_name]
  214. if step_name != "field":
  215. lst.sort(key=lambda entry: entry.name)
  216. self._lsts[step_name] = tuple(lst) # don't change any more
  217. #
  218. # check for a possible internal inconsistency: _cffi_struct_unions
  219. # should have been generated with exactly self._struct_unions
  220. lst = self._lsts["struct_union"]
  221. for tp, i in self._struct_unions.items():
  222. assert i < len(lst)
  223. assert lst[i].name == tp.name
  224. assert len(lst) == len(self._struct_unions)
  225. # same with enums
  226. lst = self._lsts["enum"]
  227. for tp, i in self._enums.items():
  228. assert i < len(lst)
  229. assert lst[i].name == tp.name
  230. assert len(lst) == len(self._enums)
  231. # ----------
  232. def _prnt(self, what=''):
  233. self._f.write(what + '\n')
  234. def write_source_to_f(self, f, preamble):
  235. if self.target_is_python:
  236. assert preamble is None
  237. self.write_py_source_to_f(f)
  238. else:
  239. assert preamble is not None
  240. self.write_c_source_to_f(f, preamble)
  241. def _rel_readlines(self, filename):
  242. g = open(os.path.join(os.path.dirname(__file__), filename), 'r')
  243. lines = g.readlines()
  244. g.close()
  245. return lines
  246. def write_c_source_to_f(self, f, preamble):
  247. self._f = f
  248. prnt = self._prnt
  249. if self.ffi._embedding is not None:
  250. prnt('#define _CFFI_USE_EMBEDDING')
  251. #
  252. # first the '#include' (actually done by inlining the file's content)
  253. lines = self._rel_readlines('_cffi_include.h')
  254. i = lines.index('#include "parse_c_type.h"\n')
  255. lines[i:i+1] = self._rel_readlines('parse_c_type.h')
  256. prnt(''.join(lines))
  257. #
  258. # if we have ffi._embedding != None, we give it here as a macro
  259. # and include an extra file
  260. base_module_name = self.module_name.split('.')[-1]
  261. if self.ffi._embedding is not None:
  262. prnt('#define _CFFI_MODULE_NAME "%s"' % (self.module_name,))
  263. prnt('static const char _CFFI_PYTHON_STARTUP_CODE[] = {')
  264. self._print_string_literal_in_array(self.ffi._embedding)
  265. prnt('0 };')
  266. prnt('#ifdef PYPY_VERSION')
  267. prnt('# define _CFFI_PYTHON_STARTUP_FUNC _cffi_pypyinit_%s' % (
  268. base_module_name,))
  269. prnt('#elif PY_MAJOR_VERSION >= 3')
  270. prnt('# define _CFFI_PYTHON_STARTUP_FUNC PyInit_%s' % (
  271. base_module_name,))
  272. prnt('#else')
  273. prnt('# define _CFFI_PYTHON_STARTUP_FUNC init%s' % (
  274. base_module_name,))
  275. prnt('#endif')
  276. lines = self._rel_readlines('_embedding.h')
  277. i = lines.index('#include "_cffi_errors.h"\n')
  278. lines[i:i+1] = self._rel_readlines('_cffi_errors.h')
  279. prnt(''.join(lines))
  280. self.needs_version(VERSION_EMBEDDED)
  281. #
  282. # then paste the C source given by the user, verbatim.
  283. prnt('/************************************************************/')
  284. prnt()
  285. prnt(preamble)
  286. prnt()
  287. prnt('/************************************************************/')
  288. prnt()
  289. #
  290. # the declaration of '_cffi_types'
  291. prnt('static void *_cffi_types[] = {')
  292. typeindex2type = dict([(i, tp) for (tp, i) in self._typesdict.items()])
  293. for i, op in enumerate(self.cffi_types):
  294. comment = ''
  295. if i in typeindex2type:
  296. comment = ' // ' + typeindex2type[i]._get_c_name()
  297. prnt('/* %2d */ %s,%s' % (i, op.as_c_expr(), comment))
  298. if not self.cffi_types:
  299. prnt(' 0')
  300. prnt('};')
  301. prnt()
  302. #
  303. # call generate_cpy_xxx_decl(), for every xxx found from
  304. # ffi._parser._declarations. This generates all the functions.
  305. self._seen_constants = set()
  306. self._generate("decl")
  307. #
  308. # the declaration of '_cffi_globals' and '_cffi_typenames'
  309. nums = {}
  310. for step_name in self.ALL_STEPS:
  311. lst = self._lsts[step_name]
  312. nums[step_name] = len(lst)
  313. if nums[step_name] > 0:
  314. prnt('static const struct _cffi_%s_s _cffi_%ss[] = {' % (
  315. step_name, step_name))
  316. for entry in lst:
  317. prnt(entry.as_c_expr())
  318. prnt('};')
  319. prnt()
  320. #
  321. # the declaration of '_cffi_includes'
  322. if self.ffi._included_ffis:
  323. prnt('static const char * const _cffi_includes[] = {')
  324. for ffi_to_include in self.ffi._included_ffis:
  325. try:
  326. included_module_name, included_source = (
  327. ffi_to_include._assigned_source[:2])
  328. except AttributeError:
  329. raise VerificationError(
  330. "ffi object %r includes %r, but the latter has not "
  331. "been prepared with set_source()" % (
  332. self.ffi, ffi_to_include,))
  333. if included_source is None:
  334. raise VerificationError(
  335. "not implemented yet: ffi.include() of a Python-based "
  336. "ffi inside a C-based ffi")
  337. prnt(' "%s",' % (included_module_name,))
  338. prnt(' NULL')
  339. prnt('};')
  340. prnt()
  341. #
  342. # the declaration of '_cffi_type_context'
  343. prnt('static const struct _cffi_type_context_s _cffi_type_context = {')
  344. prnt(' _cffi_types,')
  345. for step_name in self.ALL_STEPS:
  346. if nums[step_name] > 0:
  347. prnt(' _cffi_%ss,' % step_name)
  348. else:
  349. prnt(' NULL, /* no %ss */' % step_name)
  350. for step_name in self.ALL_STEPS:
  351. if step_name != "field":
  352. prnt(' %d, /* num_%ss */' % (nums[step_name], step_name))
  353. if self.ffi._included_ffis:
  354. prnt(' _cffi_includes,')
  355. else:
  356. prnt(' NULL, /* no includes */')
  357. prnt(' %d, /* num_types */' % (len(self.cffi_types),))
  358. flags = 0
  359. if self._num_externpy:
  360. flags |= 1 # set to mean that we use extern "Python"
  361. prnt(' %d, /* flags */' % flags)
  362. prnt('};')
  363. prnt()
  364. #
  365. # the init function
  366. prnt('#ifdef __GNUC__')
  367. prnt('# pragma GCC visibility push(default) /* for -fvisibility= */')
  368. prnt('#endif')
  369. prnt()
  370. prnt('#ifdef PYPY_VERSION')
  371. prnt('PyMODINIT_FUNC')
  372. prnt('_cffi_pypyinit_%s(const void *p[])' % (base_module_name,))
  373. prnt('{')
  374. if self._num_externpy:
  375. prnt(' if (((intptr_t)p[0]) >= 0x0A03) {')
  376. prnt(' _cffi_call_python_org = '
  377. '(void(*)(struct _cffi_externpy_s *, char *))p[1];')
  378. prnt(' }')
  379. prnt(' p[0] = (const void *)0x%x;' % self._version)
  380. prnt(' p[1] = &_cffi_type_context;')
  381. prnt('#if PY_MAJOR_VERSION >= 3')
  382. prnt(' return NULL;')
  383. prnt('#endif')
  384. prnt('}')
  385. # on Windows, distutils insists on putting init_cffi_xyz in
  386. # 'export_symbols', so instead of fighting it, just give up and
  387. # give it one
  388. prnt('# ifdef _MSC_VER')
  389. prnt(' PyMODINIT_FUNC')
  390. prnt('# if PY_MAJOR_VERSION >= 3')
  391. prnt(' PyInit_%s(void) { return NULL; }' % (base_module_name,))
  392. prnt('# else')
  393. prnt(' init%s(void) { }' % (base_module_name,))
  394. prnt('# endif')
  395. prnt('# endif')
  396. prnt('#elif PY_MAJOR_VERSION >= 3')
  397. prnt('PyMODINIT_FUNC')
  398. prnt('PyInit_%s(void)' % (base_module_name,))
  399. prnt('{')
  400. prnt(' return _cffi_init("%s", 0x%x, &_cffi_type_context);' % (
  401. self.module_name, self._version))
  402. prnt('}')
  403. prnt('#else')
  404. prnt('PyMODINIT_FUNC')
  405. prnt('init%s(void)' % (base_module_name,))
  406. prnt('{')
  407. prnt(' _cffi_init("%s", 0x%x, &_cffi_type_context);' % (
  408. self.module_name, self._version))
  409. prnt('}')
  410. prnt('#endif')
  411. prnt()
  412. prnt('#ifdef __GNUC__')
  413. prnt('# pragma GCC visibility pop')
  414. prnt('#endif')
  415. self._version = None
  416. def _to_py(self, x):
  417. if isinstance(x, str):
  418. return "b'%s'" % (x,)
  419. if isinstance(x, (list, tuple)):
  420. rep = [self._to_py(item) for item in x]
  421. if len(rep) == 1:
  422. rep.append('')
  423. return "(%s)" % (','.join(rep),)
  424. return x.as_python_expr() # Py2: unicode unexpected; Py3: bytes unexp.
  425. def write_py_source_to_f(self, f):
  426. self._f = f
  427. prnt = self._prnt
  428. #
  429. # header
  430. prnt("# auto-generated file")
  431. prnt("import _cffi_backend")
  432. #
  433. # the 'import' of the included ffis
  434. num_includes = len(self.ffi._included_ffis or ())
  435. for i in range(num_includes):
  436. ffi_to_include = self.ffi._included_ffis[i]
  437. try:
  438. included_module_name, included_source = (
  439. ffi_to_include._assigned_source[:2])
  440. except AttributeError:
  441. raise VerificationError(
  442. "ffi object %r includes %r, but the latter has not "
  443. "been prepared with set_source()" % (
  444. self.ffi, ffi_to_include,))
  445. if included_source is not None:
  446. raise VerificationError(
  447. "not implemented yet: ffi.include() of a C-based "
  448. "ffi inside a Python-based ffi")
  449. prnt('from %s import ffi as _ffi%d' % (included_module_name, i))
  450. prnt()
  451. prnt("ffi = _cffi_backend.FFI('%s'," % (self.module_name,))
  452. prnt(" _version = 0x%x," % (self._version,))
  453. self._version = None
  454. #
  455. # the '_types' keyword argument
  456. self.cffi_types = tuple(self.cffi_types) # don't change any more
  457. types_lst = [op.as_python_bytes() for op in self.cffi_types]
  458. prnt(' _types = %s,' % (self._to_py(''.join(types_lst)),))
  459. typeindex2type = dict([(i, tp) for (tp, i) in self._typesdict.items()])
  460. #
  461. # the keyword arguments from ALL_STEPS
  462. for step_name in self.ALL_STEPS:
  463. lst = self._lsts[step_name]
  464. if len(lst) > 0 and step_name != "field":
  465. prnt(' _%ss = %s,' % (step_name, self._to_py(lst)))
  466. #
  467. # the '_includes' keyword argument
  468. if num_includes > 0:
  469. prnt(' _includes = (%s,),' % (
  470. ', '.join(['_ffi%d' % i for i in range(num_includes)]),))
  471. #
  472. # the footer
  473. prnt(')')
  474. # ----------
  475. def _gettypenum(self, type):
  476. # a KeyError here is a bug. please report it! :-)
  477. return self._typesdict[type]
  478. def _convert_funcarg_to_c(self, tp, fromvar, tovar, errcode):
  479. extraarg = ''
  480. if isinstance(tp, model.BasePrimitiveType) and not tp.is_complex_type():
  481. if tp.is_integer_type() and tp.name != '_Bool':
  482. converter = '_cffi_to_c_int'
  483. extraarg = ', %s' % tp.name
  484. elif isinstance(tp, model.UnknownFloatType):
  485. # don't check with is_float_type(): it may be a 'long
  486. # double' here, and _cffi_to_c_double would loose precision
  487. converter = '(%s)_cffi_to_c_double' % (tp.get_c_name(''),)
  488. else:
  489. cname = tp.get_c_name('')
  490. converter = '(%s)_cffi_to_c_%s' % (cname,
  491. tp.name.replace(' ', '_'))
  492. if cname in ('char16_t', 'char32_t'):
  493. self.needs_version(VERSION_CHAR16CHAR32)
  494. errvalue = '-1'
  495. #
  496. elif isinstance(tp, model.PointerType):
  497. self._convert_funcarg_to_c_ptr_or_array(tp, fromvar,
  498. tovar, errcode)
  499. return
  500. #
  501. elif (isinstance(tp, model.StructOrUnionOrEnum) or
  502. isinstance(tp, model.BasePrimitiveType)):
  503. # a struct (not a struct pointer) as a function argument;
  504. # or, a complex (the same code works)
  505. self._prnt(' if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)'
  506. % (tovar, self._gettypenum(tp), fromvar))
  507. self._prnt(' %s;' % errcode)
  508. return
  509. #
  510. elif isinstance(tp, model.FunctionPtrType):
  511. converter = '(%s)_cffi_to_c_pointer' % tp.get_c_name('')
  512. extraarg = ', _cffi_type(%d)' % self._gettypenum(tp)
  513. errvalue = 'NULL'
  514. #
  515. else:
  516. raise NotImplementedError(tp)
  517. #
  518. self._prnt(' %s = %s(%s%s);' % (tovar, converter, fromvar, extraarg))
  519. self._prnt(' if (%s == (%s)%s && PyErr_Occurred())' % (
  520. tovar, tp.get_c_name(''), errvalue))
  521. self._prnt(' %s;' % errcode)
  522. def _extra_local_variables(self, tp, localvars):
  523. if isinstance(tp, model.PointerType):
  524. localvars.add('Py_ssize_t datasize')
  525. def _convert_funcarg_to_c_ptr_or_array(self, tp, fromvar, tovar, errcode):
  526. self._prnt(' datasize = _cffi_prepare_pointer_call_argument(')
  527. self._prnt(' _cffi_type(%d), %s, (char **)&%s);' % (
  528. self._gettypenum(tp), fromvar, tovar))
  529. self._prnt(' if (datasize != 0) {')
  530. self._prnt(' if (datasize < 0)')
  531. self._prnt(' %s;' % errcode)
  532. self._prnt(' %s = (%s)alloca((size_t)datasize);' % (
  533. tovar, tp.get_c_name('')))
  534. self._prnt(' memset((void *)%s, 0, (size_t)datasize);' % (tovar,))
  535. self._prnt(' if (_cffi_convert_array_from_object('
  536. '(char *)%s, _cffi_type(%d), %s) < 0)' % (
  537. tovar, self._gettypenum(tp), fromvar))
  538. self._prnt(' %s;' % errcode)
  539. self._prnt(' }')
  540. def _convert_expr_from_c(self, tp, var, context):
  541. if isinstance(tp, model.BasePrimitiveType):
  542. if tp.is_integer_type() and tp.name != '_Bool':
  543. return '_cffi_from_c_int(%s, %s)' % (var, tp.name)
  544. elif isinstance(tp, model.UnknownFloatType):
  545. return '_cffi_from_c_double(%s)' % (var,)
  546. elif tp.name != 'long double' and not tp.is_complex_type():
  547. cname = tp.name.replace(' ', '_')
  548. if cname in ('char16_t', 'char32_t'):
  549. self.needs_version(VERSION_CHAR16CHAR32)
  550. return '_cffi_from_c_%s(%s)' % (cname, var)
  551. else:
  552. return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % (
  553. var, self._gettypenum(tp))
  554. elif isinstance(tp, (model.PointerType, model.FunctionPtrType)):
  555. return '_cffi_from_c_pointer((char *)%s, _cffi_type(%d))' % (
  556. var, self._gettypenum(tp))
  557. elif isinstance(tp, model.ArrayType):
  558. return '_cffi_from_c_pointer((char *)%s, _cffi_type(%d))' % (
  559. var, self._gettypenum(model.PointerType(tp.item)))
  560. elif isinstance(tp, model.StructOrUnion):
  561. if tp.fldnames is None:
  562. raise TypeError("'%s' is used as %s, but is opaque" % (
  563. tp._get_c_name(), context))
  564. return '_cffi_from_c_struct((char *)&%s, _cffi_type(%d))' % (
  565. var, self._gettypenum(tp))
  566. elif isinstance(tp, model.EnumType):
  567. return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % (
  568. var, self._gettypenum(tp))
  569. else:
  570. raise NotImplementedError(tp)
  571. # ----------
  572. # typedefs
  573. def _typedef_type(self, tp, name):
  574. return self._global_type(tp, "(*(%s *)0)" % (name,))
  575. def _generate_cpy_typedef_collecttype(self, tp, name):
  576. self._do_collect_type(self._typedef_type(tp, name))
  577. def _generate_cpy_typedef_decl(self, tp, name):
  578. pass
  579. def _typedef_ctx(self, tp, name):
  580. type_index = self._typesdict[tp]
  581. self._lsts["typename"].append(TypenameExpr(name, type_index))
  582. def _generate_cpy_typedef_ctx(self, tp, name):
  583. tp = self._typedef_type(tp, name)
  584. self._typedef_ctx(tp, name)
  585. if getattr(tp, "origin", None) == "unknown_type":
  586. self._struct_ctx(tp, tp.name, approxname=None)
  587. elif isinstance(tp, model.NamedPointerType):
  588. self._struct_ctx(tp.totype, tp.totype.name, approxname=tp.name,
  589. named_ptr=tp)
  590. # ----------
  591. # function declarations
  592. def _generate_cpy_function_collecttype(self, tp, name):
  593. self._do_collect_type(tp.as_raw_function())
  594. if tp.ellipsis and not self.target_is_python:
  595. self._do_collect_type(tp)
  596. def _generate_cpy_function_decl(self, tp, name):
  597. assert not self.target_is_python
  598. assert isinstance(tp, model.FunctionPtrType)
  599. if tp.ellipsis:
  600. # cannot support vararg functions better than this: check for its
  601. # exact type (including the fixed arguments), and build it as a
  602. # constant function pointer (no CPython wrapper)
  603. self._generate_cpy_constant_decl(tp, name)
  604. return
  605. prnt = self._prnt
  606. numargs = len(tp.args)
  607. if numargs == 0:
  608. argname = 'noarg'
  609. elif numargs == 1:
  610. argname = 'arg0'
  611. else:
  612. argname = 'args'
  613. #
  614. # ------------------------------
  615. # the 'd' version of the function, only for addressof(lib, 'func')
  616. arguments = []
  617. call_arguments = []
  618. context = 'argument of %s' % name
  619. for i, type in enumerate(tp.args):
  620. arguments.append(type.get_c_name(' x%d' % i, context))
  621. call_arguments.append('x%d' % i)
  622. repr_arguments = ', '.join(arguments)
  623. repr_arguments = repr_arguments or 'void'
  624. if tp.abi:
  625. abi = tp.abi + ' '
  626. else:
  627. abi = ''
  628. name_and_arguments = '%s_cffi_d_%s(%s)' % (abi, name, repr_arguments)
  629. prnt('static %s' % (tp.result.get_c_name(name_and_arguments),))
  630. prnt('{')
  631. call_arguments = ', '.join(call_arguments)
  632. result_code = 'return '
  633. if isinstance(tp.result, model.VoidType):
  634. result_code = ''
  635. prnt(' %s%s(%s);' % (result_code, name, call_arguments))
  636. prnt('}')
  637. #
  638. prnt('#ifndef PYPY_VERSION') # ------------------------------
  639. #
  640. prnt('static PyObject *')
  641. prnt('_cffi_f_%s(PyObject *self, PyObject *%s)' % (name, argname))
  642. prnt('{')
  643. #
  644. context = 'argument of %s' % name
  645. for i, type in enumerate(tp.args):
  646. arg = type.get_c_name(' x%d' % i, context)
  647. prnt(' %s;' % arg)
  648. #
  649. localvars = set()
  650. for type in tp.args:
  651. self._extra_local_variables(type, localvars)
  652. for decl in localvars:
  653. prnt(' %s;' % (decl,))
  654. #
  655. if not isinstance(tp.result, model.VoidType):
  656. result_code = 'result = '
  657. context = 'result of %s' % name
  658. result_decl = ' %s;' % tp.result.get_c_name(' result', context)
  659. prnt(result_decl)
  660. else:
  661. result_decl = None
  662. result_code = ''
  663. #
  664. if len(tp.args) > 1:
  665. rng = range(len(tp.args))
  666. for i in rng:
  667. prnt(' PyObject *arg%d;' % i)
  668. prnt()
  669. prnt(' if (!PyArg_UnpackTuple(args, "%s", %d, %d, %s))' % (
  670. name, len(rng), len(rng),
  671. ', '.join(['&arg%d' % i for i in rng])))
  672. prnt(' return NULL;')
  673. prnt()
  674. #
  675. for i, type in enumerate(tp.args):
  676. self._convert_funcarg_to_c(type, 'arg%d' % i, 'x%d' % i,
  677. 'return NULL')
  678. prnt()
  679. #
  680. prnt(' Py_BEGIN_ALLOW_THREADS')
  681. prnt(' _cffi_restore_errno();')
  682. call_arguments = ['x%d' % i for i in range(len(tp.args))]
  683. call_arguments = ', '.join(call_arguments)
  684. prnt(' { %s%s(%s); }' % (result_code, name, call_arguments))
  685. prnt(' _cffi_save_errno();')
  686. prnt(' Py_END_ALLOW_THREADS')
  687. prnt()
  688. #
  689. prnt(' (void)self; /* unused */')
  690. if numargs == 0:
  691. prnt(' (void)noarg; /* unused */')
  692. if result_code:
  693. prnt(' return %s;' %
  694. self._convert_expr_from_c(tp.result, 'result', 'result type'))
  695. else:
  696. prnt(' Py_INCREF(Py_None);')
  697. prnt(' return Py_None;')
  698. prnt('}')
  699. #
  700. prnt('#else') # ------------------------------
  701. #
  702. # the PyPy version: need to replace struct/union arguments with
  703. # pointers, and if the result is a struct/union, insert a first
  704. # arg that is a pointer to the result. We also do that for
  705. # complex args and return type.
  706. def need_indirection(type):
  707. return (isinstance(type, model.StructOrUnion) or
  708. (isinstance(type, model.PrimitiveType) and
  709. type.is_complex_type()))
  710. difference = False
  711. arguments = []
  712. call_arguments = []
  713. context = 'argument of %s' % name
  714. for i, type in enumerate(tp.args):
  715. indirection = ''
  716. if need_indirection(type):
  717. indirection = '*'
  718. difference = True
  719. arg = type.get_c_name(' %sx%d' % (indirection, i), context)
  720. arguments.append(arg)
  721. call_arguments.append('%sx%d' % (indirection, i))
  722. tp_result = tp.result
  723. if need_indirection(tp_result):
  724. context = 'result of %s' % name
  725. arg = tp_result.get_c_name(' *result', context)
  726. arguments.insert(0, arg)
  727. tp_result = model.void_type
  728. result_decl = None
  729. result_code = '*result = '
  730. difference = True
  731. if difference:
  732. repr_arguments = ', '.join(arguments)
  733. repr_arguments = repr_arguments or 'void'
  734. name_and_arguments = '%s_cffi_f_%s(%s)' % (abi, name,
  735. repr_arguments)
  736. prnt('static %s' % (tp_result.get_c_name(name_and_arguments),))
  737. prnt('{')
  738. if result_decl:
  739. prnt(result_decl)
  740. call_arguments = ', '.join(call_arguments)
  741. prnt(' { %s%s(%s); }' % (result_code, name, call_arguments))
  742. if result_decl:
  743. prnt(' return result;')
  744. prnt('}')
  745. else:
  746. prnt('# define _cffi_f_%s _cffi_d_%s' % (name, name))
  747. #
  748. prnt('#endif') # ------------------------------
  749. prnt()
  750. def _generate_cpy_function_ctx(self, tp, name):
  751. if tp.ellipsis and not self.target_is_python:
  752. self._generate_cpy_constant_ctx(tp, name)
  753. return
  754. type_index = self._typesdict[tp.as_raw_function()]
  755. numargs = len(tp.args)
  756. if self.target_is_python:
  757. meth_kind = OP_DLOPEN_FUNC
  758. elif numargs == 0:
  759. meth_kind = OP_CPYTHON_BLTN_N # 'METH_NOARGS'
  760. elif numargs == 1:
  761. meth_kind = OP_CPYTHON_BLTN_O # 'METH_O'
  762. else:
  763. meth_kind = OP_CPYTHON_BLTN_V # 'METH_VARARGS'
  764. self._lsts["global"].append(
  765. GlobalExpr(name, '_cffi_f_%s' % name,
  766. CffiOp(meth_kind, type_index),
  767. size='_cffi_d_%s' % name))
  768. # ----------
  769. # named structs or unions
  770. def _field_type(self, tp_struct, field_name, tp_field):
  771. if isinstance(tp_field, model.ArrayType):
  772. actual_length = tp_field.length
  773. if actual_length == '...':
  774. ptr_struct_name = tp_struct.get_c_name('*')
  775. actual_length = '_cffi_array_len(((%s)0)->%s)' % (
  776. ptr_struct_name, field_name)
  777. tp_item = self._field_type(tp_struct, '%s[0]' % field_name,
  778. tp_field.item)
  779. tp_field = model.ArrayType(tp_item, actual_length)
  780. return tp_field
  781. def _struct_collecttype(self, tp):
  782. self._do_collect_type(tp)
  783. if self.target_is_python:
  784. # also requires nested anon struct/unions in ABI mode, recursively
  785. for fldtype in tp.anonymous_struct_fields():
  786. self._struct_collecttype(fldtype)
  787. def _struct_decl(self, tp, cname, approxname):
  788. if tp.fldtypes is None:
  789. return
  790. prnt = self._prnt
  791. checkfuncname = '_cffi_checkfld_%s' % (approxname,)
  792. prnt('_CFFI_UNUSED_FN')
  793. prnt('static void %s(%s *p)' % (checkfuncname, cname))
  794. prnt('{')
  795. prnt(' /* only to generate compile-time warnings or errors */')
  796. prnt(' (void)p;')
  797. for fname, ftype, fbitsize, fqual in tp.enumfields():
  798. try:
  799. if ftype.is_integer_type() or fbitsize >= 0:
  800. # accept all integers, but complain on float or double
  801. if fname != '':
  802. prnt(" (void)((p->%s) | 0); /* check that '%s.%s' is "
  803. "an integer */" % (fname, cname, fname))
  804. continue
  805. # only accept exactly the type declared, except that '[]'
  806. # is interpreted as a '*' and so will match any array length.
  807. # (It would also match '*', but that's harder to detect...)
  808. while (isinstance(ftype, model.ArrayType)
  809. and (ftype.length is None or ftype.length == '...')):
  810. ftype = ftype.item
  811. fname = fname + '[0]'
  812. prnt(' { %s = &p->%s; (void)tmp; }' % (
  813. ftype.get_c_name('*tmp', 'field %r'%fname, quals=fqual),
  814. fname))
  815. except VerificationError as e:
  816. prnt(' /* %s */' % str(e)) # cannot verify it, ignore
  817. prnt('}')
  818. prnt('struct _cffi_align_%s { char x; %s y; };' % (approxname, cname))
  819. prnt()
  820. def _struct_ctx(self, tp, cname, approxname, named_ptr=None):
  821. type_index = self._typesdict[tp]
  822. reason_for_not_expanding = None
  823. flags = []
  824. if isinstance(tp, model.UnionType):
  825. flags.append("_CFFI_F_UNION")
  826. if tp.fldtypes is None:
  827. flags.append("_CFFI_F_OPAQUE")
  828. reason_for_not_expanding = "opaque"
  829. if (tp not in self.ffi._parser._included_declarations and
  830. (named_ptr is None or
  831. named_ptr not in self.ffi._parser._included_declarations)):
  832. if tp.fldtypes is None:
  833. pass # opaque
  834. elif tp.partial or any(tp.anonymous_struct_fields()):
  835. pass # field layout obtained silently from the C compiler
  836. else:
  837. flags.append("_CFFI_F_CHECK_FIELDS")
  838. if tp.packed:
  839. if tp.packed > 1:
  840. raise NotImplementedError(
  841. "%r is declared with 'pack=%r'; only 0 or 1 are "
  842. "supported in API mode (try to use \"...;\", which "
  843. "does not require a 'pack' declaration)" %
  844. (tp, tp.packed))
  845. flags.append("_CFFI_F_PACKED")
  846. else:
  847. flags.append("_CFFI_F_EXTERNAL")
  848. reason_for_not_expanding = "external"
  849. flags = '|'.join(flags) or '0'
  850. c_fields = []
  851. if reason_for_not_expanding is None:
  852. expand_anonymous_struct_union = not self.target_is_python
  853. enumfields = list(tp.enumfields(expand_anonymous_struct_union))
  854. for fldname, fldtype, fbitsize, fqual in enumfields:
  855. fldtype = self._field_type(tp, fldname, fldtype)
  856. self._check_not_opaque(fldtype,
  857. "field '%s.%s'" % (tp.name, fldname))
  858. # cname is None for _add_missing_struct_unions() only
  859. op = OP_NOOP
  860. if fbitsize >= 0:
  861. op = OP_BITFIELD
  862. size = '%d /* bits */' % fbitsize
  863. elif cname is None or (
  864. isinstance(fldtype, model.ArrayType) and
  865. fldtype.length is None):
  866. size = '(size_t)-1'
  867. else:
  868. size = 'sizeof(((%s)0)->%s)' % (
  869. tp.get_c_name('*') if named_ptr is None
  870. else named_ptr.name,
  871. fldname)
  872. if cname is None or fbitsize >= 0:
  873. offset = '(size_t)-1'
  874. elif named_ptr is not None:
  875. offset = '((char *)&((%s)0)->%s) - (char *)0' % (
  876. named_ptr.name, fldname)
  877. else:
  878. offset = 'offsetof(%s, %s)' % (tp.get_c_name(''), fldname)
  879. c_fields.append(
  880. FieldExpr(fldname, offset, size, fbitsize,
  881. CffiOp(op, self._typesdict[fldtype])))
  882. first_field_index = len(self._lsts["field"])
  883. self._lsts["field"].extend(c_fields)
  884. #
  885. if cname is None: # unknown name, for _add_missing_struct_unions
  886. size = '(size_t)-2'
  887. align = -2
  888. comment = "unnamed"
  889. else:
  890. if named_ptr is not None:
  891. size = 'sizeof(*(%s)0)' % (named_ptr.name,)
  892. align = '-1 /* unknown alignment */'
  893. else:
  894. size = 'sizeof(%s)' % (cname,)
  895. align = 'offsetof(struct _cffi_align_%s, y)' % (approxname,)
  896. comment = None
  897. else:
  898. size = '(size_t)-1'
  899. align = -1
  900. first_field_index = -1
  901. comment = reason_for_not_expanding
  902. self._lsts["struct_union"].append(
  903. StructUnionExpr(tp.name, type_index, flags, size, align, comment,
  904. first_field_index, c_fields))
  905. self._seen_struct_unions.add(tp)
  906. def _check_not_opaque(self, tp, location):
  907. while isinstance(tp, model.ArrayType):
  908. tp = tp.item
  909. if isinstance(tp, model.StructOrUnion) and tp.fldtypes is None:
  910. raise TypeError(
  911. "%s is of an opaque type (not declared in cdef())" % location)
  912. def _add_missing_struct_unions(self):
  913. # not very nice, but some struct declarations might be missing
  914. # because they don't have any known C name. Check that they are
  915. # not partial (we can't complete or verify them!) and emit them
  916. # anonymously.
  917. lst = list(self._struct_unions.items())
  918. lst.sort(key=lambda tp_order: tp_order[1])
  919. for tp, order in lst:
  920. if tp not in self._seen_struct_unions:
  921. if tp.partial:
  922. raise NotImplementedError("internal inconsistency: %r is "
  923. "partial but was not seen at "
  924. "this point" % (tp,))
  925. if tp.name.startswith('$') and tp.name[1:].isdigit():
  926. approxname = tp.name[1:]
  927. elif tp.name == '_IO_FILE' and tp.forcename == 'FILE':
  928. approxname = 'FILE'
  929. self._typedef_ctx(tp, 'FILE')
  930. else:
  931. raise NotImplementedError("internal inconsistency: %r" %
  932. (tp,))
  933. self._struct_ctx(tp, None, approxname)
  934. def _generate_cpy_struct_collecttype(self, tp, name):
  935. self._struct_collecttype(tp)
  936. _generate_cpy_union_collecttype = _generate_cpy_struct_collecttype
  937. def _struct_names(self, tp):
  938. cname = tp.get_c_name('')
  939. if ' ' in cname:
  940. return cname, cname.replace(' ', '_')
  941. else:
  942. return cname, '_' + cname
  943. def _generate_cpy_struct_decl(self, tp, name):
  944. self._struct_decl(tp, *self._struct_names(tp))
  945. _generate_cpy_union_decl = _generate_cpy_struct_decl
  946. def _generate_cpy_struct_ctx(self, tp, name):
  947. self._struct_ctx(tp, *self._struct_names(tp))
  948. _generate_cpy_union_ctx = _generate_cpy_struct_ctx
  949. # ----------
  950. # 'anonymous' declarations. These are produced for anonymous structs
  951. # or unions; the 'name' is obtained by a typedef.
  952. def _generate_cpy_anonymous_collecttype(self, tp, name):
  953. if isinstance(tp, model.EnumType):
  954. self._generate_cpy_enum_collecttype(tp, name)
  955. else:
  956. self._struct_collecttype(tp)
  957. def _generate_cpy_anonymous_decl(self, tp, name):
  958. if isinstance(tp, model.EnumType):
  959. self._generate_cpy_enum_decl(tp)
  960. else:
  961. self._struct_decl(tp, name, 'typedef_' + name)
  962. def _generate_cpy_anonymous_ctx(self, tp, name):
  963. if isinstance(tp, model.EnumType):
  964. self._enum_ctx(tp, name)
  965. else:
  966. self._struct_ctx(tp, name, 'typedef_' + name)
  967. # ----------
  968. # constants, declared with "static const ..."
  969. def _generate_cpy_const(self, is_int, name, tp=None, category='const',
  970. check_value=None):
  971. if (category, name) in self._seen_constants:
  972. raise VerificationError(
  973. "duplicate declaration of %s '%s'" % (category, name))
  974. self._seen_constants.add((category, name))
  975. #
  976. prnt = self._prnt
  977. funcname = '_cffi_%s_%s' % (category, name)
  978. if is_int:
  979. prnt('static int %s(unsigned long long *o)' % funcname)
  980. prnt('{')
  981. prnt(' int n = (%s) <= 0;' % (name,))
  982. prnt(' *o = (unsigned long long)((%s) | 0);'
  983. ' /* check that %s is an integer */' % (name, name))
  984. if check_value is not None:
  985. if check_value > 0:
  986. check_value = '%dU' % (check_value,)
  987. prnt(' if (!_cffi_check_int(*o, n, %s))' % (check_value,))
  988. prnt(' n |= 2;')
  989. prnt(' return n;')
  990. prnt('}')
  991. else:
  992. assert check_value is None
  993. prnt('static void %s(char *o)' % funcname)
  994. prnt('{')
  995. prnt(' *(%s)o = %s;' % (tp.get_c_name('*'), name))
  996. prnt('}')
  997. prnt()
  998. def _generate_cpy_constant_collecttype(self, tp, name):
  999. is_int = tp.is_integer_type()
  1000. if not is_int or self.target_is_python:
  1001. self._do_collect_type(tp)
  1002. def _generate_cpy_constant_decl(self, tp, name):
  1003. is_int = tp.is_integer_type()
  1004. self._generate_cpy_const(is_int, name, tp)
  1005. def _generate_cpy_constant_ctx(self, tp, name):
  1006. if not self.target_is_python and tp.is_integer_type():
  1007. type_op = CffiOp(OP_CONSTANT_INT, -1)
  1008. else:
  1009. if self.target_is_python:
  1010. const_kind = OP_DLOPEN_CONST
  1011. else:
  1012. const_kind = OP_CONSTANT
  1013. type_index = self._typesdict[tp]
  1014. type_op = CffiOp(const_kind, type_index)
  1015. self._lsts["global"].append(
  1016. GlobalExpr(name, '_cffi_const_%s' % name, type_op))
  1017. # ----------
  1018. # enums
  1019. def _generate_cpy_enum_collecttype(self, tp, name):
  1020. self._do_collect_type(tp)
  1021. def _generate_cpy_enum_decl(self, tp, name=None):
  1022. for enumerator in tp.enumerators:
  1023. self._generate_cpy_const(True, enumerator)
  1024. def _enum_ctx(self, tp, cname):
  1025. type_index = self._typesdict[tp]
  1026. type_op = CffiOp(OP_ENUM, -1)
  1027. if self.target_is_python:
  1028. tp.check_not_partial()
  1029. for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues):
  1030. self._lsts["global"].append(
  1031. GlobalExpr(enumerator, '_cffi_const_%s' % enumerator, type_op,
  1032. check_value=enumvalue))
  1033. #
  1034. if cname is not None and '$' not in cname and not self.target_is_python:
  1035. size = "sizeof(%s)" % cname
  1036. signed = "((%s)-1) <= 0" % cname
  1037. else:
  1038. basetp = tp.build_baseinttype(self.ffi, [])
  1039. size = self.ffi.sizeof(basetp)
  1040. signed = int(int(self.ffi.cast(basetp, -1)) < 0)
  1041. allenums = ",".join(tp.enumerators)
  1042. self._lsts["enum"].append(
  1043. EnumExpr(tp.name, type_index, size, signed, allenums))
  1044. def _generate_cpy_enum_ctx(self, tp, name):
  1045. self._enum_ctx(tp, tp._get_c_name())
  1046. # ----------
  1047. # macros: for now only for integers
  1048. def _generate_cpy_macro_collecttype(self, tp, name):
  1049. pass
  1050. def _generate_cpy_macro_decl(self, tp, name):
  1051. if tp == '...':
  1052. check_value = None
  1053. else:
  1054. check_value = tp # an integer
  1055. self._generate_cpy_const(True, name, check_value=check_value)
  1056. def _generate_cpy_macro_ctx(self, tp, name):
  1057. if tp == '...':
  1058. if self.target_is_python:
  1059. raise VerificationError(
  1060. "cannot use the syntax '...' in '#define %s ...' when "
  1061. "using the ABI mode" % (name,))
  1062. check_value = None
  1063. else:
  1064. check_value = tp # an integer
  1065. type_op = CffiOp(OP_CONSTANT_INT, -1)
  1066. self._lsts["global"].append(
  1067. GlobalExpr(name, '_cffi_const_%s' % name, type_op,
  1068. check_value=check_value))
  1069. # ----------
  1070. # global variables
  1071. def _global_type(self, tp, global_name):
  1072. if isinstance(tp, model.ArrayType):
  1073. actual_length = tp.length
  1074. if actual_length == '...':
  1075. actual_length = '_cffi_array_len(%s)' % (global_name,)
  1076. tp_item = self._global_type(tp.item, '%s[0]' % global_name)
  1077. tp = model.ArrayType(tp_item, actual_length)
  1078. return tp
  1079. def _generate_cpy_variable_collecttype(self, tp, name):
  1080. self._do_collect_type(self._global_type(tp, name))
  1081. def _generate_cpy_variable_decl(self, tp, name):
  1082. prnt = self._prnt
  1083. tp = self._global_type(tp, name)
  1084. if isinstance(tp, model.ArrayType) and tp.length is None:
  1085. tp = tp.item
  1086. ampersand = ''
  1087. else:
  1088. ampersand = '&'
  1089. # This code assumes that casts from "tp *" to "void *" is a
  1090. # no-op, i.e. a function that returns a "tp *" can be called
  1091. # as if it returned a "void *". This should be generally true
  1092. # on any modern machine. The only exception to that rule (on
  1093. # uncommon architectures, and as far as I can tell) might be
  1094. # if 'tp' were a function type, but that is not possible here.
  1095. # (If 'tp' is a function _pointer_ type, then casts from "fn_t
  1096. # **" to "void *" are again no-ops, as far as I can tell.)
  1097. decl = '*_cffi_var_%s(void)' % (name,)
  1098. prnt('static ' + tp.get_c_name(decl, quals=self._current_quals))
  1099. prnt('{')
  1100. prnt(' return %s(%s);' % (ampersand, name))
  1101. prnt('}')
  1102. prnt()
  1103. def _generate_cpy_variable_ctx(self, tp, name):
  1104. tp = self._global_type(tp, name)
  1105. type_index = self._typesdict[tp]
  1106. if self.target_is_python:
  1107. op = OP_GLOBAL_VAR
  1108. else:
  1109. op = OP_GLOBAL_VAR_F
  1110. self._lsts["global"].append(
  1111. GlobalExpr(name, '_cffi_var_%s' % name, CffiOp(op, type_index)))
  1112. # ----------
  1113. # extern "Python"
  1114. def _generate_cpy_extern_python_collecttype(self, tp, name):
  1115. assert isinstance(tp, model.FunctionPtrType)
  1116. self._do_collect_type(tp)
  1117. _generate_cpy_dllexport_python_collecttype = \
  1118. _generate_cpy_extern_python_plus_c_collecttype = \
  1119. _generate_cpy_extern_python_collecttype
  1120. def _extern_python_decl(self, tp, name, tag_and_space):
  1121. prnt = self._prnt
  1122. if isinstance(tp.result, model.VoidType):
  1123. size_of_result = '0'
  1124. else:
  1125. context = 'result of %s' % name
  1126. size_of_result = '(int)sizeof(%s)' % (
  1127. tp.result.get_c_name('', context),)
  1128. prnt('static struct _cffi_externpy_s _cffi_externpy__%s =' % name)
  1129. prnt(' { "%s.%s", %s };' % (self.module_name, name, size_of_result))
  1130. prnt()
  1131. #
  1132. arguments = []
  1133. context = 'argument of %s' % name
  1134. for i, type in enumerate(tp.args):
  1135. arg = type.get_c_name(' a%d' % i, context)
  1136. arguments.append(arg)
  1137. #
  1138. repr_arguments = ', '.join(arguments)
  1139. repr_arguments = repr_arguments or 'void'
  1140. name_and_arguments = '%s(%s)' % (name, repr_arguments)
  1141. if tp.abi == "__stdcall":
  1142. name_and_arguments = '_cffi_stdcall ' + name_and_arguments
  1143. #
  1144. def may_need_128_bits(tp):
  1145. return (isinstance(tp, model.PrimitiveType) and
  1146. tp.name == 'long double')
  1147. #
  1148. size_of_a = max(len(tp.args)*8, 8)
  1149. if may_need_128_bits(tp.result):
  1150. size_of_a = max(size_of_a, 16)
  1151. if isinstance(tp.result, model.StructOrUnion):
  1152. size_of_a = 'sizeof(%s) > %d ? sizeof(%s) : %d' % (
  1153. tp.result.get_c_name(''), size_of_a,
  1154. tp.result.get_c_name(''), size_of_a)
  1155. prnt('%s%s' % (tag_and_space, tp.result.get_c_name(name_and_arguments)))
  1156. prnt('{')
  1157. prnt(' char a[%s];' % size_of_a)
  1158. prnt(' char *p = a;')
  1159. for i, type in enumerate(tp.args):
  1160. arg = 'a%d' % i
  1161. if (isinstance(type, model.StructOrUnion) or
  1162. may_need_128_bits(type)):
  1163. arg = '&' + arg
  1164. type = model.PointerType(type)
  1165. prnt(' *(%s)(p + %d) = %s;' % (type.get_c_name('*'), i*8, arg))
  1166. prnt(' _cffi_call_python(&_cffi_externpy__%s, p);' % name)
  1167. if not isinstance(tp.result, model.VoidType):
  1168. prnt(' return *(%s)p;' % (tp.result.get_c_name('*'),))
  1169. prnt('}')
  1170. prnt()
  1171. self._num_externpy += 1
  1172. def _generate_cpy_extern_python_decl(self, tp, name):
  1173. self._extern_python_decl(tp, name, 'static ')
  1174. def _generate_cpy_dllexport_python_decl(self, tp, name):
  1175. self._extern_python_decl(tp, name, 'CFFI_DLLEXPORT ')
  1176. def _generate_cpy_extern_python_plus_c_decl(self, tp, name):
  1177. self._extern_python_decl(tp, name, '')
  1178. def _generate_cpy_extern_python_ctx(self, tp, name):
  1179. if self.target_is_python:
  1180. raise VerificationError(
  1181. "cannot use 'extern \"Python\"' in the ABI mode")
  1182. if tp.ellipsis:
  1183. raise NotImplementedError("a vararg function is extern \"Python\"")
  1184. type_index = self._typesdict[tp]
  1185. type_op = CffiOp(OP_EXTERN_PYTHON, type_index)
  1186. self._lsts["global"].append(
  1187. GlobalExpr(name, '&_cffi_externpy__%s' % name, type_op, name))
  1188. _generate_cpy_dllexport_python_ctx = \
  1189. _generate_cpy_extern_python_plus_c_ctx = \
  1190. _generate_cpy_extern_python_ctx
  1191. def _print_string_literal_in_array(self, s):
  1192. prnt = self._prnt
  1193. prnt('// # NB. this is not a string because of a size limit in MSVC')
  1194. for line in s.splitlines(True):
  1195. prnt(('// ' + line).rstrip())
  1196. printed_line = ''
  1197. for c in line:
  1198. if len(printed_line) >= 76:
  1199. prnt(printed_line)
  1200. printed_line = ''
  1201. printed_line += '%d,' % (ord(c),)
  1202. prnt(printed_line)
  1203. # ----------
  1204. # emitting the opcodes for individual types
  1205. def _emit_bytecode_VoidType(self, tp, index):
  1206. self.cffi_types[index] = CffiOp(OP_PRIMITIVE, PRIM_VOID)
  1207. def _emit_bytecode_PrimitiveType(self, tp, index):
  1208. prim_index = PRIMITIVE_TO_INDEX[tp.name]
  1209. self.cffi_types[index] = CffiOp(OP_PRIMITIVE, prim_index)
  1210. def _emit_bytecode_UnknownIntegerType(self, tp, index):
  1211. s = ('_cffi_prim_int(sizeof(%s), (\n'
  1212. ' ((%s)-1) | 0 /* check that %s is an integer type */\n'
  1213. ' ) <= 0)' % (tp.name, tp.name, tp.name))
  1214. self.cffi_types[index] = CffiOp(OP_PRIMITIVE, s)
  1215. def _emit_bytecode_UnknownFloatType(self, tp, index):
  1216. s = ('_cffi_prim_float(sizeof(%s) *\n'
  1217. ' (((%s)1) / 2) * 2 /* integer => 0, float => 1 */\n'
  1218. ' )' % (tp.name, tp.name))
  1219. self.cffi_types[index] = CffiOp(OP_PRIMITIVE, s)
  1220. def _emit_bytecode_RawFunctionType(self, tp, index):
  1221. self.cffi_types[index] = CffiOp(OP_FUNCTION, self._typesdict[tp.result])
  1222. index += 1
  1223. for tp1 in tp.args:
  1224. realindex = self._typesdict[tp1]
  1225. if index != realindex:
  1226. if isinstance(tp1, model.PrimitiveType):
  1227. self._emit_bytecode_PrimitiveType(tp1, index)
  1228. else:
  1229. self.cffi_types[index] = CffiOp(OP_NOOP, realindex)
  1230. index += 1
  1231. flags = int(tp.ellipsis)
  1232. if tp.abi is not None:
  1233. if tp.abi == '__stdcall':
  1234. flags |= 2
  1235. else:
  1236. raise NotImplementedError("abi=%r" % (tp.abi,))
  1237. self.cffi_types[index] = CffiOp(OP_FUNCTION_END, flags)
  1238. def _emit_bytecode_PointerType(self, tp, index):
  1239. self.cffi_types[index] = CffiOp(OP_POINTER, self._typesdict[tp.totype])
  1240. _emit_bytecode_ConstPointerType = _emit_bytecode_PointerType
  1241. _emit_bytecode_NamedPointerType = _emit_bytecode_PointerType
  1242. def _emit_bytecode_FunctionPtrType(self, tp, index):
  1243. raw = tp.as_raw_function()
  1244. self.cffi_types[index] = CffiOp(OP_POINTER, self._typesdict[raw])
  1245. def _emit_bytecode_ArrayType(self, tp, index):
  1246. item_index = self._typesdict[tp.item]
  1247. if tp.length is None:
  1248. self.cffi_types[index] = CffiOp(OP_OPEN_ARRAY, item_index)
  1249. elif tp.length == '...':
  1250. raise VerificationError(
  1251. "type %s badly placed: the '...' array length can only be "
  1252. "used on global arrays or on fields of structures" % (
  1253. str(tp).replace('/*...*/', '...'),))
  1254. else:
  1255. assert self.cffi_types[index + 1] == 'LEN'
  1256. self.cffi_types[index] = CffiOp(OP_ARRAY, item_index)
  1257. self.cffi_types[index + 1] = CffiOp(None, str(tp.length))
  1258. def _emit_bytecode_StructType(self, tp, index):
  1259. struct_index = self._struct_unions[tp]
  1260. self.cffi_types[index] = CffiOp(OP_STRUCT_UNION, struct_index)
  1261. _emit_bytecode_UnionType = _emit_bytecode_StructType
  1262. def _emit_bytecode_EnumType(self, tp, index):
  1263. enum_index = self._enums[tp]
  1264. self.cffi_types[index] = CffiOp(OP_ENUM, enum_index)
  1265. if sys.version_info >= (3,):
  1266. NativeIO = io.StringIO
  1267. else:
  1268. class NativeIO(io.BytesIO):
  1269. def write(self, s):
  1270. if isinstance(s, unicode):
  1271. s = s.encode('ascii')
  1272. super(NativeIO, self).write(s)
  1273. def _make_c_or_py_source(ffi, module_name, preamble, target_file, verbose):
  1274. if verbose:
  1275. print("generating %s" % (target_file,))
  1276. recompiler = Recompiler(ffi, module_name,
  1277. target_is_python=(preamble is None))
  1278. recompiler.collect_type_table()
  1279. recompiler.collect_step_tables()
  1280. f = NativeIO()
  1281. recompiler.write_source_to_f(f, preamble)
  1282. output = f.getvalue()
  1283. try:
  1284. with open(target_file, 'r') as f1:
  1285. if f1.read(len(output) + 1) != output:
  1286. raise IOError
  1287. if verbose:
  1288. print("(already up-to-date)")
  1289. return False # already up-to-date
  1290. except IOError:
  1291. tmp_file = '%s.~%d' % (target_file, os.getpid())
  1292. with open(tmp_file, 'w') as f1:
  1293. f1.write(output)
  1294. try:
  1295. os.rename(tmp_file, target_file)
  1296. except OSError:
  1297. os.unlink(target_file)
  1298. os.rename(tmp_file, target_file)
  1299. return True
  1300. def make_c_source(ffi, module_name, preamble, target_c_file, verbose=False):
  1301. assert preamble is not None
  1302. return _make_c_or_py_source(ffi, module_name, preamble, target_c_file,
  1303. verbose)
  1304. def make_py_source(ffi, module_name, target_py_file, verbose=False):
  1305. return _make_c_or_py_source(ffi, module_name, None, target_py_file,
  1306. verbose)
  1307. def _modname_to_file(outputdir, modname, extension):
  1308. parts = modname.split('.')
  1309. try:
  1310. os.makedirs(os.path.join(outputdir, *parts[:-1]))
  1311. except OSError:
  1312. pass
  1313. parts[-1] += extension
  1314. return os.path.join(outputdir, *parts), parts
  1315. # Aaargh. Distutils is not tested at all for the purpose of compiling
  1316. # DLLs that are not extension modules. Here are some hacks to work
  1317. # around that, in the _patch_for_*() functions...
  1318. def _patch_meth(patchlist, cls, name, new_meth):
  1319. old = getattr(cls, name)
  1320. patchlist.append((cls, name, old))
  1321. setattr(cls, name, new_meth)
  1322. return old
  1323. def _unpatch_meths(patchlist):
  1324. for cls, name, old_meth in reversed(patchlist):
  1325. setattr(cls, name, old_meth)
  1326. def _patch_for_embedding(patchlist):
  1327. if sys.platform == 'win32':
  1328. # we must not remove the manifest when building for embedding!
  1329. from distutils.msvc9compiler import MSVCCompiler
  1330. _patch_meth(patchlist, MSVCCompiler, '_remove_visual_c_ref',
  1331. lambda self, manifest_file: manifest_file)
  1332. if sys.platform == 'darwin':
  1333. # we must not make a '-bundle', but a '-dynamiclib' instead
  1334. from distutils.ccompiler import CCompiler
  1335. def my_link_shared_object(self, *args, **kwds):
  1336. if '-bundle' in self.linker_so:
  1337. self.linker_so = list(self.linker_so)
  1338. i = self.linker_so.index('-bundle')
  1339. self.linker_so[i] = '-dynamiclib'
  1340. return old_link_shared_object(self, *args, **kwds)
  1341. old_link_shared_object = _patch_meth(patchlist, CCompiler,
  1342. 'link_shared_object',
  1343. my_link_shared_object)
  1344. def _patch_for_target(patchlist, target):
  1345. from distutils.command.build_ext import build_ext
  1346. # if 'target' is different from '*', we need to patch some internal
  1347. # method to just return this 'target' value, instead of having it
  1348. # built from module_name
  1349. if target.endswith('.*'):
  1350. target = target[:-2]
  1351. if sys.platform == 'win32':
  1352. target += '.dll'
  1353. elif sys.platform == 'darwin':
  1354. target += '.dylib'
  1355. else:
  1356. target += '.so'
  1357. _patch_meth(patchlist, build_ext, 'get_ext_filename',
  1358. lambda self, ext_name: target)
  1359. def recompile(ffi, module_name, preamble, tmpdir='.', call_c_compiler=True,
  1360. c_file=None, source_extension='.c', extradir=None,
  1361. compiler_verbose=1, target=None, debug=None, **kwds):
  1362. if not isinstance(module_name, str):
  1363. module_name = module_name.encode('ascii')
  1364. if ffi._windows_unicode:
  1365. ffi._apply_windows_unicode(kwds)
  1366. if preamble is not None:
  1367. embedding = (ffi._embedding is not None)
  1368. if embedding:
  1369. ffi._apply_embedding_fix(kwds)
  1370. if c_file is None:
  1371. c_file, parts = _modname_to_file(tmpdir, module_name,
  1372. source_extension)
  1373. if extradir:
  1374. parts = [extradir] + parts
  1375. ext_c_file = os.path.join(*parts)
  1376. else:
  1377. ext_c_file = c_file
  1378. #
  1379. if target is None:
  1380. if embedding:
  1381. target = '%s.*' % module_name
  1382. else:
  1383. target = '*'
  1384. #
  1385. ext = ffiplatform.get_extension(ext_c_file, module_name, **kwds)
  1386. updated = make_c_source(ffi, module_name, preamble, c_file,
  1387. verbose=compiler_verbose)
  1388. if call_c_compiler:
  1389. patchlist = []
  1390. cwd = os.getcwd()
  1391. try:
  1392. if embedding:
  1393. _patch_for_embedding(patchlist)
  1394. if target != '*':
  1395. _patch_for_target(patchlist, target)
  1396. if compiler_verbose:
  1397. if tmpdir == '.':
  1398. msg = 'the current directory is'
  1399. else:
  1400. msg = 'setting the current directory to'
  1401. print('%s %r' % (msg, os.path.abspath(tmpdir)))
  1402. os.chdir(tmpdir)
  1403. outputfilename = ffiplatform.compile('.', ext,
  1404. compiler_verbose, debug)
  1405. finally:
  1406. os.chdir(cwd)
  1407. _unpatch_meths(patchlist)
  1408. return outputfilename
  1409. else:
  1410. return ext, updated
  1411. else:
  1412. if c_file is None:
  1413. c_file, _ = _modname_to_file(tmpdir, module_name, '.py')
  1414. updated = make_py_source(ffi, module_name, c_file,
  1415. verbose=compiler_verbose)
  1416. if call_c_compiler:
  1417. return c_file
  1418. else:
  1419. return None, updated