Commits

Armin Rigo committed fa4cf4d

Random check-in changing things here and there.

Comments (0)

Files changed (6)

                 return self._parsed_types[cdecl]
             except KeyError:
                 type = self._parser.parse_type(cdecl)
-                btype = type.get_backend_type(self)
+                btype = self._get_cached_btype(type)
                 self._parsed_types[cdecl] = btype
                 return btype
         else:
             BType = self._new_types[cdecl]
         except KeyError:
             type = self._parser.parse_type(cdecl, force_pointer=True)
-            BType = type.get_backend_type(self)
+            BType = self._get_cached_btype(type)
             self._new_types[cdecl] = BType
         #
         return self._backend.newp(BType, init)
             if decl.name:
                 self._declare('variable ' + decl.name, self._get_type(node))
 
-    def parse_type(self, cdecl, force_pointer=False,
-                   convert_array_to_pointer=False):
+    def parse_type(self, cdecl, force_pointer=False):
         # XXX: for more efficiency we would need to poke into the
         # internals of CParser...  the following registers the
         # typedefs, because their presence or absence influences the
         csourcelines.append('void __dummy(%s);' % cdecl)
         ast = _get_parser().parse('\n'.join(csourcelines))
         typenode = ast.ext[-1].type.args.params[0].type
-        return self._get_type(typenode, force_pointer=force_pointer,
-                              convert_array_to_pointer=convert_array_to_pointer)
+        return self._get_type(typenode, force_pointer=force_pointer)
 
     def _declare(self, name, obj):
         if name in self._declarations:
         #
         raise api.FFIError("bad or unsupported type declaration")
 
-    def _parse_function_type(self, typenode, name=None):
+    def _parse_function_type(self, typenode, funcname=None):
         params = list(getattr(typenode.args, 'params', []))
         ellipsis = (
             len(params) > 0 and
                                convert_array_to_pointer=True)
                 for argdeclnode in params]
         result = self._get_type(typenode.type)
-        return model.FunctionType(name, tuple(args), result, ellipsis)
+        return model.FunctionType(tuple(args), result, ellipsis)
 
     def _get_struct_or_union_type(self, kind, type, typenode=None):
         # First, a level of caching on the exact 'type' node of the AST.

cffi/ffiplatform.py

 
-from platformer import udir
 
 class VerificationError(Exception):
     """ An error raised when verification fails
 
 class BaseType(object):
+
+    def __repr__(self):
+        return '<%s>' % (self.get_c_name(),)
+
     def __eq__(self, other):
         return (self.__class__ == other.__class__ and
                 self._get_items() == other._get_items())
         except KeyError:
             return self.new_backend_type(ffi, *args)
 
-    def get_backend_type(self, ffi):
-        return ffi._get_cached_btype(self)
-
     def verifier_declare(self, verifier, f):
         # nothing to see here
         pass
 
 class VoidType(BaseType):
     _attrs_ = ()
-    name = 'void'
-    
+
+    def get_c_name(self, replace_with=''):
+        return 'void' + replace_with
+
     def new_backend_type(self, ffi):
         return ffi._backend.new_void_type()
 
-    def __repr__(self):
-        return '<void>'
-
 void_type = VoidType()
 
 class PrimitiveType(BaseType):
     def __init__(self, name):
         self.name = name
 
+    def get_c_name(self, replace_with=''):
+        return self.name + replace_with
+
     def new_backend_type(self, ffi):
         return ffi._backend.new_primitive_type(self.name)
 
-    def __repr__(self):
-        return '<%s>' % (self.name,)
-
 class FunctionType(BaseType):
     _attrs_ = ('args', 'result', 'ellipsis')
 
-    def __init__(self, name, args, result, ellipsis):
-        self.name = name # can be None in case it's an empty type
+    def __init__(self, args, result, ellipsis):
         self.args = args
         self.result = result
         self.ellipsis = ellipsis
 
-    def __repr__(self):
-        args = ', '.join([repr(x) for x in self.args])
+    def get_c_name(self, replace_with=''):
+        reprargs = [arg.get_c_name() for arg in self.args]
         if self.ellipsis:
-            return '<(%s, ...) -> %r>' % (args, self.result)
-        return '<(%s) -> %r>' % (args, self.result)
+            reprargs.append('...')
+        replace_with = '(*%s)(%s)' % (replace_with, ', '.join(reprargs))
+        return self.result.get_c_name(replace_with)
 
     def prepare_backend_type(self, ffi):
         args = [ffi._get_cached_btype(self.result)]
         for arg in self.args:
             args.append(arg.name)
         args = ', '.join(args)
-        f.write('  %s(* res%d)(%s) = %s;\n' % (restype, verifier.rescount,
-                                               args, self.name))
-        verifier.rescount += 1
+        f.write('  { %s(* result)(%s) = %s; }\n' % (restype,
+                                                    args, self.name))
 
 class PointerType(BaseType):
     _attrs_ = ('totype',)
     def __init__(self, totype):
         self.totype = totype
 
+    def get_c_name(self, replace_with=''):
+        return self.totype.get_c_name('* ' + replace_with)
+
     def prepare_backend_type(self, ffi):
         return (ffi._get_cached_btype(self.totype),)
 
     def new_backend_type(self, ffi, BItem):
         return ffi._backend.new_pointer_type(BItem)
 
-    def __repr__(self):
-        return '<*%r>' % (self.totype,)
-
 class ArrayType(BaseType):
     _attrs_ = ('item', 'length')
 
     def __init__(self, item, length):
-        self.item = PointerType(item) # XXX why is this pointer?
+        self.item = item
         self.length = length
 
-    def __repr__(self):
+    def get_c_name(self, replace_with=''):
         if self.length is None:
-            return '<%r[]>' % (self.item,)
-        return '<%r[%s]>' % (self.item, self.length)
+            brackets = '[]'
+        else:
+            brackets = '[%d]' % self.length
+        return self.item.get_c_name(replace_with + brackets)
 
     def prepare_backend_type(self, ffi):
-        return (ffi._get_cached_btype(self.item),)
+        return (ffi._get_cached_btype(PointerType(self.item)),)
 
-    def new_backend_type(self, ffi, BItem):
-        return ffi._backend.new_array_type(BItem, self.length)
+    def new_backend_type(self, ffi, BPtrItem):
+        return ffi._backend.new_array_type(BPtrItem, self.length)
 
 class StructOrUnion(BaseType):
     _attrs_ = ('name',)
         self.fldtypes = fldtypes
         self.fldbitsize = fldbitsize
 
-    def __repr__(self):
-        if self.fldnames is None:
-            return '<struct %s>' % (self.name,)
-        fldrepr = ', '.join(['%s: %r' % (name, tp) for name, tp in
-                             zip(self.fldnames, self.fldtypes)])
-        return '<struct %s {%s}>' % (self.name, fldrepr)
+    def get_c_name(self, replace_with=''):
+        return '%s %s%s' % (self.kind, self.name, replace_with)
 
     def prepare_backend_type(self, ffi):
         BType = self.get_btype(ffi)
         return BType
 
 class StructType(StructOrUnion):
+    kind = 'struct'
+
     def get_btype(self, ffi):
         return ffi._backend.new_struct_type(self.name)
 
         verifier._write_printf(f, 'END struct %s' % self.name)
 
 class UnionType(StructOrUnion):
+    kind = 'union'
+
     def get_btype(self, ffi):
         return ffi._backend.new_union_type(self.name)
     
         self.enumerators = enumerators
         self.enumvalues = enumvalues
 
+    def get_c_name(self, replace_with=''):
+        return 'enum %s%s' % (self.name, replace_with)
+
     def new_backend_type(self, ffi):
         return ffi._backend.new_enum_type(self.name, self.enumerators,
                                           self.enumvalues)
 
-from platformer import platform, ExternalCompilationInfo
 from . import ffiplatform
 
 class Verifier(object):
-    def __init__(self):
-        self.rescount = 0
 
     def _write_printf(f, what, *args):
         if not args:

testing/test_verify.py

-
 import py
-from cffi import FFI
-from platformer import CompilationError
+from cffi import FFI, VerificationError
 
 def test_simple_verify():
     ffi = FFI()
     ffi.cdef("void some_completely_unknown_function();")
-    py.test.raises(CompilationError, ffi.verify)
+    py.test.raises(VerificationError, ffi.verify)
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     # omission of math.h
-    py.test.raises(CompilationError, ffi.verify)
+    py.test.raises(VerificationError, ffi.verify)
     assert ffi.verify('#include <math.h>') is None
     #
     ffi = FFI()
     ffi.cdef("float sin(double x);")
-    py.test.raises(CompilationError, ffi.verify, '#include <math.h>')
+    py.test.raises(VerificationError, ffi.verify, '#include <math.h>')
     ffi = FFI()
     ffi.cdef("double sin(float x);")
-    py.test.raises(CompilationError, ffi.verify, '#include <math.h>')
+    py.test.raises(VerificationError, ffi.verify, '#include <math.h>')