[pypy-commit] cffi cffi-1.0: Tweaks to use a model that is closer to the existing one of cffi than an

arigo noreply at buildbot.pypy.org
Wed Apr 15 19:58:27 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1718:eec843d871af
Date: 2015-04-15 19:59 +0200
http://bitbucket.org/cffi/cffi/changeset/eec843d871af/

Log:	Tweaks to use a model that is closer to the existing one of cffi
	than an ideal model would be

diff --git a/new/cffi_opcode.py b/new/cffi_opcode.py
--- a/new/cffi_opcode.py
+++ b/new/cffi_opcode.py
@@ -19,15 +19,14 @@
 OP_OPEN_ARRAY      = 7
 OP_STRUCT_UNION    = 9
 OP_ENUM            = 11
-OP_TYPENAME        = 13
-OP_FUNCTION        = 15
-OP_FUNCTION_END    = 17
-OP_NOOP            = 19
-OP_BITFIELD        = 21
-OP_INTEGER_CONST   = 23
-OP_CPYTHON_BLTN_V  = 25   # varargs
-OP_CPYTHON_BLTN_N  = 27   # noargs
-OP_CPYTHON_BLTN_O  = 29   # O  (i.e. a single arg)
+OP_FUNCTION        = 13
+OP_FUNCTION_END    = 15
+OP_NOOP            = 17
+OP_BITFIELD        = 19
+OP_INTEGER_CONST   = 21
+OP_CPYTHON_BLTN_V  = 23   # varargs
+OP_CPYTHON_BLTN_N  = 25   # noargs
+OP_CPYTHON_BLTN_O  = 27   # O  (i.e. a single arg)
 
 PRIM_VOID          = 0
 PRIM_BOOL          = 1
@@ -45,12 +44,54 @@
 PRIM_FLOAT         = 13
 PRIM_DOUBLE        = 14
 PRIM_LONGDOUBLE    = 15
-_NUM_PRIM          = 16
+
+PRIM_WCHAR         = 16
+PRIM_INT8          = 17
+PRIM_UINT8         = 18
+PRIM_INT16         = 19
+PRIM_UINT16        = 20
+PRIM_INT32         = 21
+PRIM_UINT32        = 22
+PRIM_INT64         = 23
+PRIM_UINT64        = 24
+PRIM_INTPTR        = 25
+PRIM_UINTPTR       = 26
+PRIM_PTRDIFF       = 27
+PRIM_SIZE          = 28
+PRIM_SSIZE         = 29
+
+_NUM_PRIM          = 30
 
 PRIMITIVE_TO_INDEX = {
-    'int':    PRIM_INT,
-    'float':  PRIM_FLOAT,
-    'double': PRIM_DOUBLE,
+    'char':               PRIM_CHAR,
+    'short':              PRIM_SHORT,
+    'int':                PRIM_INT,
+    'long':               PRIM_LONG,
+    'long long':          PRIM_LONGLONG,
+    'signed char':        PRIM_SCHAR,
+    'unsigned char':      PRIM_UCHAR,
+    'unsigned short':     PRIM_USHORT,
+    'unsigned int':       PRIM_UINT,
+    'unsigned long':      PRIM_ULONG,
+    'unsigned long long': PRIM_ULONGLONG,
+    'float':              PRIM_FLOAT,
+    'double':             PRIM_DOUBLE,
+    'long double':        PRIM_LONGDOUBLE,
+    '_Bool':              PRIM_BOOL,
+    'wchar_t':            PRIM_WCHAR,
+    'int8_t':             PRIM_INT8,
+    'uint8_t':            PRIM_UINT8,
+    'int16_t':            PRIM_INT16,
+    'uint16_t':           PRIM_UINT16,
+    'int32_t':            PRIM_INT32,
+    'uint32_t':           PRIM_UINT32,
+    'int64_t':            PRIM_INT64,
+    'uint64_t':           PRIM_UINT64,
+    'intptr_t':           PRIM_INTPTR,
+    'uintptr_t':          PRIM_UINTPTR,
+    'ptrdiff_t':          PRIM_PTRDIFF,
+    'size_t':             PRIM_SIZE,
+    'ssize_t':            PRIM_SSIZE,
     }
 
 CLASS_NAME = {}
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
@@ -506,7 +506,8 @@
             if (n < 0)
                 return parse_error(tok, "undefined type name");
 
-            t1 = _CFFI_OP(_CFFI_OP_TYPENAME, n);
+            t1 = _CFFI_OP(_CFFI_OP_NOOP,
+                          tok->info->ctx->typenames[n].type_index);
             break;
         }
         case TOK_STRUCT:
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
@@ -13,15 +13,14 @@
 #define _CFFI_OP_OPEN_ARRAY      7
 #define _CFFI_OP_STRUCT_UNION    9
 #define _CFFI_OP_ENUM           11
-#define _CFFI_OP_TYPENAME       13
-#define _CFFI_OP_FUNCTION       15
-#define _CFFI_OP_FUNCTION_END   17
-#define _CFFI_OP_NOOP           19
-#define _CFFI_OP_BITFIELD       21
-#define _CFFI_OP_INTEGER_CONST  23
-#define _CFFI_OP_CPYTHON_BLTN_V 25   // varargs
-#define _CFFI_OP_CPYTHON_BLTN_N 27   // noargs
-#define _CFFI_OP_CPYTHON_BLTN_O 29   // O  (i.e. a single arg)
+#define _CFFI_OP_FUNCTION       13
+#define _CFFI_OP_FUNCTION_END   15
+#define _CFFI_OP_NOOP           17
+#define _CFFI_OP_BITFIELD       19
+#define _CFFI_OP_INTEGER_CONST  21
+#define _CFFI_OP_CPYTHON_BLTN_V 23   // varargs
+#define _CFFI_OP_CPYTHON_BLTN_N 25   // noargs
+#define _CFFI_OP_CPYTHON_BLTN_O 27   // O  (i.e. a single arg)
 
 #define _CFFI_PRIM_VOID          0
 #define _CFFI_PRIM_BOOL          1
@@ -39,7 +38,23 @@
 #define _CFFI_PRIM_FLOAT        13
 #define _CFFI_PRIM_DOUBLE       14
 #define _CFFI_PRIM_LONGDOUBLE   15
-#define _CFFI__NUM_PRIM         16
+
+#define _CFFI_PRIM_WCHAR        16
+#define _CFFI_PRIM_INT8         17
+#define _CFFI_PRIM_UINT8        18
+#define _CFFI_PRIM_INT16        19
+#define _CFFI_PRIM_UINT16       20
+#define _CFFI_PRIM_INT32        21
+#define _CFFI_PRIM_UINT32       22
+#define _CFFI_PRIM_INT64        23
+#define _CFFI_PRIM_UINT64       24
+#define _CFFI_PRIM_INTPTR       25
+#define _CFFI_PRIM_UINTPTR      26
+#define _CFFI_PRIM_PTRDIFF      27
+#define _CFFI_PRIM_SIZE         28
+#define _CFFI_PRIM_SSIZE        29
+
+#define _CFFI__NUM_PRIM         30
 
 
 struct _cffi_global_s {
@@ -79,7 +94,8 @@
 
 struct _cffi_typename_s {
     const char *name;
-    _cffi_opcode_t type_op;   /* 0 if opaque or prebuilt */
+    int type_index;   /* if opaque, points to a possibly artificial
+                         OP_STRUCT which is itself opaque */
 };
 
 struct _cffi_type_context_s {
diff --git a/new/recompiler.py b/new/recompiler.py
--- a/new/recompiler.py
+++ b/new/recompiler.py
@@ -11,7 +11,7 @@
 
     def collect_type_table(self):
         self._typesdict = {}
-        self._generate('collecttype')
+        self._generate("collecttype")
         #
         all_decls = sorted(self._typesdict, key=str)
         #
@@ -262,6 +262,12 @@
             raise NotImplementedError(tp)
 
     # ----------
+    # typedefs
+
+    def _generate_cpy_typedef_collecttype(self, tp, name):
+        self._do_collect_type(tp)
+
+    # ----------
     # function declarations
 
     def _generate_cpy_function_collecttype(self, tp, name):
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
@@ -37,6 +37,7 @@
 ctx_identifiers = ffi.new("struct _cffi_typename_s[]", len(identifier_names))
 for _i in range(len(identifier_names)):
     ctx_identifiers[_i].name = c_identifier_names[_i]
+    ctx_identifiers[_i].type_index = 100 + _i
 ctx.typenames = ctx_identifiers
 ctx.num_typenames = len(identifier_names)
 
@@ -92,7 +93,6 @@
 Func = make_getter('FUNCTION')
 FuncEnd = make_getter('FUNCTION_END')
 Struct = make_getter('STRUCT_UNION')
-Typename = make_getter('TYPENAME')
 
 
 def test_simple():
@@ -239,12 +239,14 @@
 
 def test_identifier():
     for i in range(len(identifier_names)):
-        assert parse("%s" % (identifier_names[i])) == ['->', Typename(i)]
-        assert parse("%s*" % (identifier_names[i])) == [Typename(i),
+        assert parse("%s" % (identifier_names[i])) == ['->', NoOp(100 + i)]
+        assert parse("%s*" % (identifier_names[i])) == [NoOp(100 + i),
                                                         '->', Pointer(0)]
 
 def test_cffi_opcode_sync():
-    import cffi_opcode
+    import cffi_opcode, cffi1.model
     for name in dir(lib):
         if name.startswith('_CFFI_'):
             assert getattr(cffi_opcode, name[6:]) == getattr(lib, name)
+    assert sorted(cffi_opcode.PRIMITIVE_TO_INDEX.keys()) == (
+        sorted(cffi1.model.PrimitiveType.ALL_PRIMITIVE_TYPES.keys()))
diff --git a/new/test_recompiler.py b/new/test_recompiler.py
--- a/new/test_recompiler.py
+++ b/new/test_recompiler.py
@@ -44,10 +44,22 @@
                      "(FUNCTION 7)(POINTER 0)(FUNCTION_END 0)"
                      "(PRIMITIVE 14)(PRIMITIVE 7)")
 
+def test_variadic_function():
+    check_type_table("int sin(int, ...);",
+                     "(FUNCTION 1)(PRIMITIVE 7)(FUNCTION_END 1)")
+
 def test_array():
     check_type_table("int a[100];",
                      "(PRIMITIVE 7)(ARRAY 0)(None 100)")
 
+def test_typedef():
+    check_type_table("typedef int foo_t;",
+                     "(PRIMITIVE 7)")
+
+def test_prebuilt_type():
+    check_type_table("int32_t f(void);",
+                     "(FUNCTION 2)(FUNCTION_END 0)(PRIMITIVE 21)")
+
 
 def test_math_sin():
     ffi = FFI()


More information about the pypy-commit mailing list