[pypy-commit] creflect default: more primitive types

arigo noreply at buildbot.pypy.org
Sat Nov 29 09:54:57 CET 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r107:0fab3ed49da1
Date: 2014-11-29 09:55 +0100
http://bitbucket.org/cffi/creflect/changeset/0fab3ed49da1/

Log:	more primitive types

diff --git a/creflect/src/c_decl_parser.c b/creflect/src/c_decl_parser.c
--- a/creflect/src/c_decl_parser.c
+++ b/creflect/src/c_decl_parser.c
@@ -282,6 +282,12 @@
     }
 }
 
+static crx_type_t *parse_error(crxp_token_t *tok)
+{
+    tok->kind = TOK_ERROR;
+    return NULL;
+}
+
 static crx_type_t *parse_complete(crxp_token_t *tok)
 {
     crx_type_t *t1;
@@ -289,18 +295,91 @@
     if (is_const) {
         next_token(tok);
     }
+
+    int modifiers_length = 0;
+    int modifiers_sign = 0;
+ modifiers:
     switch (tok->kind) {
-    case TOK_INT:
-        t1 = tok->cb->get_signed_type(tok->cb, sizeof(int), "int");
+
+    case TOK_SHORT:
+        if (modifiers_length != 0)
+            return parse_error(tok);
+        modifiers_length--;
+        next_token(tok);
+        goto modifiers;
+
+    case TOK_LONG:
+        if (modifiers_length != 0 && modifiers_length != 1)
+            return parse_error(tok);
+        modifiers_length++;
+        next_token(tok);
+        goto modifiers;
+
+    case TOK_SIGNED:
+        if (modifiers_sign)
+            return parse_error(tok);
+        modifiers_sign++;
+        next_token(tok);
+        goto modifiers;
+
+    case TOK_UNSIGNED:
+        if (modifiers_sign)
+            return parse_error(tok);
+        modifiers_sign--;
+        next_token(tok);
+        goto modifiers;
+
+    default:
         break;
-    case TOK_VOID:
-        t1 = tok->cb->get_void_type(tok->cb);
-        break;
-    default:
-        tok->kind = TOK_ERROR;
-        return;
     }
-    next_token(tok);
+
+    if (modifiers_length || modifiers_sign) {
+        size_t size;
+        char *name;
+
+        switch (tok->kind) {
+
+        case TOK_CHAR:
+            if (modifiers_length != 0)
+                return parse_error(tok);
+            size = sizeof(char);
+            name = "char";
+            next_token(tok);
+            break;
+
+        case TOK_INT:
+            next_token(tok);
+            /* fall-through */
+        default:
+            switch (modifiers_length) {
+            case -1: size = sizeof(short);     name = "short"; break;
+            case 1:  size = sizeof(long);      name = "long"; break;
+            case 2:  size = sizeof(long long); name = "long long"; break;
+            default: size = sizeof(int);       name = "int"; break;
+            }
+        }
+        if (modifiers_sign < 0)
+            t1 = tok->cb->get_unsigned_type(tok->cb, size, name);
+        else
+            t1 = tok->cb->get_signed_type(tok->cb, size, name);
+    }
+    else {
+        switch (tok->kind) {
+        case TOK_INT:
+            t1 = tok->cb->get_signed_type(tok->cb, sizeof(int), "int");
+            break;
+        case TOK_CHAR:
+            t1 = tok->cb->get_char_type(tok->cb);
+            break;
+        case TOK_VOID:
+            t1 = tok->cb->get_void_type(tok->cb);
+            break;
+        default:
+            tok->kind = TOK_ERROR;
+            return NULL;
+        }
+        next_token(tok);
+    }
 
     if (is_const) {
         t1 = tok->cb->get_const_type(tok->cb, t1);
diff --git a/test/codegen/003i.c b/test/codegen/003i.c
new file mode 100644
--- /dev/null
+++ b/test/codegen/003i.c
@@ -0,0 +1,18 @@
+typedef signed char num_t;
+
+# ____________________________________________________________
+
+void test003i(crx_builder_t *cb)
+{
+    crx_type_t *t1;
+    {
+        num_t *p1;
+        char b[sizeof(*p1)];
+        p1 = (void *)b;
+        (void)(*p1 << 1);  /* check that 'num_t' is an integer type */
+        *p1 = -1;  /* check that 'num_t' is not declared 'const' */
+        t1 = CRX_INT_TYPE(cb, *p1, "char");
+        cb->define_type(cb, "num_t", t1);
+#expect TYPEDEF num_t = signed char
+    }
+}
diff --git a/test/codegen/glob-003j.c b/test/codegen/glob-003j.c
new file mode 100644
--- /dev/null
+++ b/test/codegen/glob-003j.c
@@ -0,0 +1,15 @@
+signed char const ab = 42;
+
+# ____________________________________________________________
+
+void testglob_003j(crx_builder_t *cb)
+{
+    crx_type_t *t1;
+    {
+        crx_num_const_t v;
+        (void)((ab) << 1);  /* check that 'ab' is an integer */
+        t1 = CRX_INT_CONST(cb, ab, &v, 1);
+        cb->define_num_const(cb, "ab", t1, &v);
+#expect NUMCONST ab = int 42
+    }
+}
diff --git a/test/test_c_decl_parser.py b/test/test_c_decl_parser.py
--- a/test/test_c_decl_parser.py
+++ b/test/test_c_decl_parser.py
@@ -62,12 +62,19 @@
     parse("int(int)", "FUNC( int -> int )")
     parse("int(int *, int const *)", "FUNC( PTR int -> PTR CONST int -> int )")
     parse("int(*)(int)", "PTR FUNC( int -> int )")
+    parse("unsigned int", "unsigned int")
+    parse("long", "long")
+    parse("long int", "long")
+    parse("short int", "short")
+    parse("long long int", "long long")
+    parse("unsigned long long *", "PTR unsigned long long")
+    parse("const unsigned long long *", "PTR CONST unsigned long long")
+    parse("unsigned long long const *", "PTR CONST unsigned long long")
+    parse("char", "char")
+    parse("signed char", "signed char")
+    parse("unsigned char", "unsigned char")
+    parse("char(*(*)(long))(int)","PTR FUNC( long -> PTR FUNC( int -> char ) )")
     import py; py.test.skip("in-progress")
-    parse("unsigned int")
-    parse("unsigned long long *")
-    parse("const unsigned long long *")
-    parse("unsigned long long const *")
-    parse("char(*(*)(long))(int)")
     parse("foo_t[]")
     parse("int(int, ...)")
     parse("int(int[])")


More information about the pypy-commit mailing list