[pypy-commit] cffi cffi-1.0: Generate struct _cffi_typename_s

arigo noreply at buildbot.pypy.org
Wed Apr 15 20:09:43 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1719:31ea57b2d53d
Date: 2015-04-15 20:10 +0200
http://bitbucket.org/cffi/cffi/changeset/31ea57b2d53d/

Log:	Generate struct _cffi_typename_s

diff --git a/new/ffi_obj.c b/new/ffi_obj.c
--- a/new/ffi_obj.c
+++ b/new/ffi_obj.c
@@ -64,7 +64,7 @@
         const void *mem[] = {ffi->info.ctx->types,
                              ffi->info.ctx->globals,
                              ffi->info.ctx->constants,
-                             ffi->info.ctx->structs_unions,
+                             ffi->info.ctx->struct_unions,
                              ffi->info.ctx->fields,
                              ffi->info.ctx->enums,
                              ffi->info.ctx->typenames,
diff --git a/new/parse_c_type.c b/new/parse_c_type.c
--- a/new/parse_c_type.c
+++ b/new/parse_c_type.c
@@ -371,7 +371,7 @@
   }
 
 MAKE_SEARCH_FUNC(globals)
-MAKE_SEARCH_FUNC(structs_unions)
+MAKE_SEARCH_FUNC(struct_unions)
 MAKE_SEARCH_FUNC(typenames)
 
 #undef MAKE_SEARCH_FUNC
@@ -518,10 +518,10 @@
             if (tok->kind != TOK_IDENTIFIER)
                 return parse_error(tok, "struct or union name expected");
 
-            int n = search_in_structs_unions(tok->info->ctx, tok->p, tok->size);
+            int n = search_in_struct_unions(tok->info->ctx, tok->p, tok->size);
             if (n < 0)
                 return parse_error(tok, "undefined struct/union name");
-            if (((tok->info->ctx->structs_unions[n].flags & CT_UNION) != 0)
+            if (((tok->info->ctx->struct_unions[n].flags & CT_UNION) != 0)
                 ^ (kind == TOK_UNION))
                 return parse_error(tok, "wrong kind of tag: struct vs union");
 
diff --git a/new/parse_c_type.h b/new/parse_c_type.h
--- a/new/parse_c_type.h
+++ b/new/parse_c_type.h
@@ -102,13 +102,13 @@
     _cffi_opcode_t *types;
     const struct _cffi_global_s *globals;
     const struct _cffi_constant_s *constants;
-    const struct _cffi_struct_union_s *structs_unions;
+    const struct _cffi_struct_union_s *struct_unions;
     const struct _cffi_field_s *fields;
     const struct _cffi_enum_s *enums;
     const struct _cffi_typename_s *typenames;
     int num_globals;
     int num_constants;
-    int num_structs_unions;
+    int num_struct_unions;
     int num_enums;
     int num_typenames;
 };
diff --git a/new/recompiler.py b/new/recompiler.py
--- a/new/recompiler.py
+++ b/new/recompiler.py
@@ -116,54 +116,42 @@
         # ffi._parser._declarations.  This generates all the functions.
         self._generate("decl")
         #
-        # the declaration of '_cffi_globals'
-        self._lst = []
-        self._generate("global")
-        num_globals = len(self._lst)
-        if num_globals > 0:
-            self._lst.sort()  # sort by name, which is at the start of each line
-            prnt('static const struct _cffi_global_s _cffi_globals[] = {')
-            for line in self._lst:
-                prnt(line)
-            prnt('};')
-            prnt()
+        # the declaration of '_cffi_globals' and '_cffi_typenames'
+        nums = {}
+        self._lsts = {"global": [], "typename": []}
+        self._generate("ctx")
+        for step_name in ["global", "typename"]:
+            lst = self._lsts[step_name]
+            nums[step_name] = len(lst)
+            if nums[step_name] > 0:
+                lst.sort()  # sort by name, which is at the start of each line
+                prnt('static const struct _cffi_%s_s _cffi_%s[] = {' % (
+                    step_name, step_name))
+                for line in lst:
+                    prnt(line)
+                prnt('};')
+                prnt()
         #
         # XXX
-        num_constants = 0
-        num_structs_unions = 0
-        num_enums = 0
-        num_typenames = 0
+        nums['constant'] = 0
+        nums['struct_union'] = 0
+        nums['enum'] = 0
         #
         # the declaration of '_cffi_type_context'
         prnt('static const struct _cffi_type_context_s _cffi_type_context = {')
         prnt('  _cffi_types,')
-        if num_globals > 0:
-            prnt('  _cffi_globals,')
-        else:
-            prnt('  NULL,  /* no globals */')
-        if num_constants > 0:
-            prnt('  _cffi_constants,')
-        else:
-            prnt('  NULL,  /* no constants */')
-        if num_structs_unions > 0:
-            prnt('  _cffi_structs_unions,')
-            prnt('  _cffi_fields,')
-        else:
-            prnt('  NULL,  /* no structs */')
-            prnt('  NULL,  /* no fields */')
-        if num_enums > 0:
-            prnt('  _cffi_enums,')
-        else:
-            prnt('  NULL,  /* no enums */')
-        if num_typenames > 0:
-            prnt('  _cffi_typenames,')
-        else:
-            prnt('  NULL,  /* no typenames */')
-        prnt('  %d,  /* num_globals */' % num_globals)
-        prnt('  %d,  /* num_constants */' % num_constants)
-        prnt('  %d,  /* num_structs_unions */' % num_structs_unions)
-        prnt('  %d,  /* num_enums */' % num_enums)
-        prnt('  %d,  /* num_typenames */' % num_typenames)
+        ALL_STEPS = ["global", "constant", "struct_union", "enum", "typename"]
+        for step_name in ALL_STEPS:
+            if nums[step_name] > 0:
+                prnt('  _cffi_%ss,' % step_name)
+                if step_name == 'struct_union':
+                    prnt('  _cffi_fields,')
+            else:
+                prnt('  NULL,  /* no %ss */' % step_name)
+                if step_name == 'struct_union':
+                    prnt('  NULL,  /* no fields */')
+        for step_name in ALL_STEPS:
+            prnt('  %d,  /* num_%ss */' % (nums[step_name], step_name))
         prnt('};')
         prnt()
         #
@@ -267,6 +255,14 @@
     def _generate_cpy_typedef_collecttype(self, tp, name):
         self._do_collect_type(tp)
 
+    def _generate_cpy_typedef_decl(self, tp, name):
+        pass
+
+    def _generate_cpy_typedef_ctx(self, tp, name):
+        type_index = self._typesdict[tp]
+        self._lsts["typename"].append(
+            '  { "%s", %d },' % (name, type_index))
+
     # ----------
     # function declarations
 
@@ -346,7 +342,7 @@
         prnt('}')
         prnt()
 
-    def _generate_cpy_function_global(self, tp, name):
+    def _generate_cpy_function_ctx(self, tp, name):
         if tp.ellipsis:
             XXX
         type_index = self._typesdict[tp.as_raw_function()]
@@ -357,7 +353,7 @@
             meth_kind = 'O'   # 'METH_O'
         else:
             meth_kind = 'V'   # 'METH_VARARGS'
-        self._lst.append(
+        self._lsts["global"].append(
             '  { "%s", _cffi_f_%s, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_%s, %d)},'
             % (name, name, meth_kind, type_index))
 
@@ -370,9 +366,9 @@
     def _generate_cpy_variable_decl(self, tp, name):
         pass
 
-    def _generate_cpy_variable_global(self, tp, name):
+    def _generate_cpy_variable_ctx(self, tp, name):
         type_index = self._typesdict[tp]
-        self._lst.append(
+        self._lsts["global"].append(
             '  { "%s", &%s, _CFFI_OP(_CFFI_OP_NOOP, %d)},'
             % (name, name, type_index))
 
diff --git a/new/test_parse_c_type.py b/new/test_parse_c_type.py
--- a/new/test_parse_c_type.py
+++ b/new/test_parse_c_type.py
@@ -30,8 +30,8 @@
 for _i in range(len(struct_names)):
     ctx_structs[_i].name = c_struct_names[_i]
 ctx_structs[3].flags = lib.CT_UNION
-ctx.structs_unions = ctx_structs
-ctx.num_structs_unions = len(struct_names)
+ctx.struct_unions = ctx_structs
+ctx.num_struct_unions = len(struct_names)
 
 c_identifier_names = [ffi.new("char[]", _n) for _n in identifier_names]
 ctx_identifiers = ffi.new("struct _cffi_typename_s[]", len(identifier_names))
diff --git a/new/test_recompiler.py b/new/test_recompiler.py
--- a/new/test_recompiler.py
+++ b/new/test_recompiler.py
@@ -70,3 +70,8 @@
     ffi = FFI()
     ffi.cdef("int a[100];")
     make_c_source(ffi, str(udir.join('global_var_array.c')), 'int a[100];')
+
+def test_typedef():
+    ffi = FFI()
+    ffi.cdef("typedef int **foo_t;")
+    make_c_source(ffi, str(udir.join('typedef.c')), 'typedef int **foo_t;')


More information about the pypy-commit mailing list