]> git.saurik.com Git - cycript.git/blobdiff - Decode.cpp
Don't crash if ivar_getTypeEncoding returns NULL.
[cycript.git] / Decode.cpp
index 7901515ba39be2d7cfa5e67dca094e4737e607a2..bdef46b80e71727190797e95fddb55cb6c808322 100644 (file)
@@ -26,6 +26,8 @@
 
 CYTypedIdentifier *Decode_(CYPool &pool, struct sig::Type *type) {
     switch (type->primitive) {
+        case sig::unknown_P: return $ CYTypedIdentifier($ CYTypeError());
+
         case sig::function_P: {
             _assert(type->data.signature.count != 0);
             CYTypedParameter *parameter(NULL);
@@ -34,52 +36,60 @@ CYTypedIdentifier *Decode_(CYPool &pool, struct sig::Type *type) {
             return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeFunctionWith(parameter));
         } break;
 
-        case sig::typename_P: return $ CYTypedIdentifier($V("Class"));
+        case sig::typename_P: return $ CYTypedIdentifier($ CYTypeVariable("Class"));
         case sig::union_P: _assert(false); break;
-        case sig::string_P: return $ CYTypedIdentifier($V("char"), $ CYTypeConstant($ CYTypePointerTo()));
-        case sig::selector_P: return $ CYTypedIdentifier($V("SEL"));
-        case sig::block_P: _assert(false); break;
+        case sig::string_P: return $ CYTypedIdentifier($ CYTypeVariable("char"), $ CYTypePointerTo());
+        case sig::selector_P: return $ CYTypedIdentifier($ CYTypeVariable("SEL"));
+
+        case sig::block_P: {
+            if (type->data.signature.count == 0)
+                return $ CYTypedIdentifier($ CYTypeVariable("NSBlock"), $ CYTypePointerTo());
+            else {
+                CYTypedParameter *parameter(NULL);
+                for (size_t i(type->data.signature.count - 1); i != 0; --i)
+                    parameter = $ CYTypedParameter(Decode(pool, type->data.signature.elements[i].type), parameter);
+                return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeBlockWith(parameter));
+            }
+        } break;
 
         case sig::object_P: {
             if (type->name == NULL)
-                return $ CYTypedIdentifier($V("id"));
+                return $ CYTypedIdentifier($ CYTypeVariable("id"));
             else
-                return $ CYTypedIdentifier($V(type->name), $ CYTypePointerTo());
+                return $ CYTypedIdentifier($ CYTypeVariable(type->name), $ CYTypePointerTo());
         } break;
 
-        case sig::boolean_P: return $ CYTypedIdentifier($V("bool"));
-        case sig::uchar_P: return $ CYTypedIdentifier($V("uchar"));
-        case sig::uint_P: return $ CYTypedIdentifier($V("uint"));
-        case sig::ulong_P: return $ CYTypedIdentifier($V("ulong"));
-        case sig::ulonglong_P: return $ CYTypedIdentifier($V("ulonglong"));
-        case sig::ushort_P: return $ CYTypedIdentifier($V("ushort"));
+        case sig::boolean_P: return $ CYTypedIdentifier($ CYTypeVariable("bool"));
+        case sig::uchar_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeVariable("char")));
+        case sig::uint_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeVariable("int")));
+        case sig::ulong_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeLong($ CYTypeVariable("int"))));
+        case sig::ulonglong_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeLong($ CYTypeLong($ CYTypeVariable("int")))));
+        case sig::ushort_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeShort($ CYTypeVariable("int"))));
         case sig::array_P: return Decode(pool, type->data.data.type)->Modify($ CYTypeArrayOf($D(type->data.data.size)));
 
-        // XXX: once again, the issue of void here is incorrect
         case sig::pointer_P: {
             CYTypedIdentifier *typed;
             if (type->data.data.type == NULL)
-                typed = $ CYTypedIdentifier($V("void"));
+                typed = $ CYTypedIdentifier($ CYTypeVoid());
             else
                 typed = Decode(pool, type->data.data.type);
             return typed->Modify($ CYTypePointerTo());
         } break;
 
         case sig::bit_P: _assert(false); break;
-        case sig::char_P: return $ CYTypedIdentifier($V("char"));
-        case sig::double_P: return $ CYTypedIdentifier($V("double"));
-        case sig::float_P: return $ CYTypedIdentifier($V("float"));
-        case sig::int_P: return $ CYTypedIdentifier($V("int"));
-        case sig::long_P: return $ CYTypedIdentifier($V("long"));
-        case sig::longlong_P: return $ CYTypedIdentifier($V("longlong"));
-        case sig::short_P: return $ CYTypedIdentifier($V("short"));
+        case sig::char_P: return $ CYTypedIdentifier($ CYTypeVariable("char"));
+        case sig::double_P: return $ CYTypedIdentifier($ CYTypeVariable("double"));
+        case sig::float_P: return $ CYTypedIdentifier($ CYTypeVariable("float"));
+        case sig::int_P: return $ CYTypedIdentifier($ CYTypeVariable("int"));
+        case sig::long_P: return $ CYTypedIdentifier($ CYTypeLong($ CYTypeVariable("int")));
+        case sig::longlong_P: return $ CYTypedIdentifier($ CYTypeLong($ CYTypeLong($ CYTypeVariable("int"))));
+        case sig::short_P: return $ CYTypedIdentifier($ CYTypeShort($ CYTypeVariable("int")));
 
-        // XXX: this happens to work, but is totally wrong
-        case sig::void_P: return $ CYTypedIdentifier($V("void"));
+        case sig::void_P: return $ CYTypedIdentifier($ CYTypeVoid());
 
         case sig::struct_P: {
             _assert(type->name != NULL);
-            return $ CYTypedIdentifier($V(type->name));
+            return $ CYTypedIdentifier($ CYTypeVariable(type->name));
         } break;
     }
 
@@ -89,7 +99,11 @@ CYTypedIdentifier *Decode_(CYPool &pool, struct sig::Type *type) {
 
 CYTypedIdentifier *Decode(CYPool &pool, struct sig::Type *type) {
     CYTypedIdentifier *typed(Decode_(pool, type));
-    if ((type->flags & JOC_TYPE_CONST) != 0)
-        typed = typed->Modify($ CYTypeConstant());
+    if ((type->flags & JOC_TYPE_CONST) != 0) {
+        if (type->primitive == sig::string_P)
+            typed->modifier_ = $ CYTypeConstant(typed->modifier_);
+        else
+            typed = typed->Modify($ CYTypeConstant());
+    }
     return typed;
 }