Commits

Armin Rigo committed e6ef781

Reorganize things a little bit

Comments (0)

Files changed (3)

         ast, macros = self._parse('void __dummy(%s);' % cdecl)
         assert not macros
         typenode = ast.ext[-1].type.args.params[0].type
-        type = self._get_type(typenode, force_pointer=force_pointer)
-        if consider_function_as_funcptr:
-            if isinstance(type, model.RawFunctionType):
-                type = self._get_type_pointer(type)
-        return type
+        return self._get_type(typenode, force_pointer=force_pointer,
+                     consider_function_as_funcptr=consider_function_as_funcptr)
 
     def _declare(self, name, obj):
         if name in self._declarations:
         return model.PointerType(type)
 
     def _get_type(self, typenode, convert_array_to_pointer=False,
-                  force_pointer=False, name=None, partial_length_ok=False):
+                  force_pointer=False, name=None, partial_length_ok=False,
+                  consider_function_as_funcptr=False):
         # first, dereference typedefs, if we have it already parsed, we're good
         if (isinstance(typenode, pycparser.c_ast.TypeDecl) and
             isinstance(typenode.type, pycparser.c_ast.IdentifierType) and
             else:
                 if force_pointer:
                     return self._get_type_pointer(type)
+                if (consider_function_as_funcptr and
+                        isinstance(type, model.RawFunctionType)):
+                    return type.as_function_pointer()
             return type
         #
         if isinstance(typenode, pycparser.c_ast.ArrayDecl):
             return model.ArrayType(self._get_type(typenode.type), length)
         #
         if force_pointer:
-            return model.PointerType(self._get_type(typenode))
+            return self._get_type_pointer(self._get_type(typenode))
         #
         if isinstance(typenode, pycparser.c_ast.PtrDecl):
             # pointer type
         #
         if isinstance(typenode, pycparser.c_ast.FuncDecl):
             # a function type
-            return self._parse_function_type(typenode, name)
+            result = self._parse_function_type(typenode, name)
+            if consider_function_as_funcptr:
+                result = result.as_function_pointer()
+            return result
         #
         raise api.FFIError("bad or unsupported type declaration")
 
                 and list(params[0].type.type.names) == ['void']):
             del params[0]
         args = [self._get_type(argdeclnode.type,
-                               convert_array_to_pointer=True)
+                               convert_array_to_pointer=True,
+                               consider_function_as_funcptr=True)
                 for argdeclnode in params]
         result = self._get_type(typenode.type)
         return model.RawFunctionType(tuple(args), result, ellipsis)
         result = ffi._get_cached_btype(self.result)
         args = []
         for tp in self.args:
-            if isinstance(tp, RawFunctionType):
-                tp = tp.as_function_pointer()
             args.append(ffi._get_cached_btype(tp))
         return global_cache(ffi, 'new_function_type',
                             tuple(args), result, self.ellipsis)
             self.prnt('    %s;' % errcode)
             return
         #
-        elif isinstance(tp, model.BaseFunctionType):
-            if isinstance(tp, model.RawFunctionType):
-                tp = tp.as_function_pointer()
+        elif isinstance(tp, model.FunctionPtrType):
             converter = '(%s)_cffi_to_c_pointer' % tp.get_c_name('')
             extraarg = ', _cffi_type(%d)' % self.gettypenum(tp)
             errvalue = 'NULL'
         prnt('{')
         #
         for i, type in enumerate(tp.args):
-            if isinstance(type, model.RawFunctionType):
-                type = type.as_function_pointer()
             prnt('  %s;' % type.get_c_name(' x%d' % i))
         if not isinstance(tp.result, model.VoidType):
             result_code = 'result = '