]> git.saurik.com Git - cycript.git/blob - Decode.cpp
Fix pointer crashes and round-trip const void *.
[cycript.git] / Decode.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <sstream>
23
24 #include "Decode.hpp"
25 #include "Replace.hpp"
26
27 CYTypedIdentifier *Decode_(CYPool &pool, struct sig::Type *type) {
28 switch (type->primitive) {
29 case sig::function_P: {
30 _assert(type->data.signature.count != 0);
31 CYTypedParameter *parameter(NULL);
32 for (size_t i(type->data.signature.count - 1); i != 0; --i)
33 parameter = $ CYTypedParameter(Decode(pool, type->data.signature.elements[i].type), parameter);
34 return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeFunctionWith(parameter));
35 } break;
36
37 case sig::typename_P: return $ CYTypedIdentifier($ CYTypeVariable("Class"));
38 case sig::union_P: _assert(false); break;
39 case sig::string_P: return $ CYTypedIdentifier($ CYTypeVariable("char"), $ CYTypePointerTo());
40 case sig::selector_P: return $ CYTypedIdentifier($ CYTypeVariable("SEL"));
41
42 case sig::block_P: {
43 _assert(type->data.signature.count != 0);
44 CYTypedParameter *parameter(NULL);
45 for (size_t i(type->data.signature.count - 1); i != 0; --i)
46 parameter = $ CYTypedParameter(Decode(pool, type->data.signature.elements[i].type), parameter);
47 return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeBlockWith(parameter));
48 } break;
49
50 case sig::object_P: {
51 if (type->name == NULL)
52 return $ CYTypedIdentifier($ CYTypeVariable("id"));
53 else
54 return $ CYTypedIdentifier($ CYTypeVariable(type->name), $ CYTypePointerTo());
55 } break;
56
57 case sig::boolean_P: return $ CYTypedIdentifier($ CYTypeVariable("bool"));
58 case sig::uchar_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeVariable("char")));
59 case sig::uint_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeVariable("int")));
60 case sig::ulong_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeLong($ CYTypeVariable("int"))));
61 case sig::ulonglong_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeLong($ CYTypeLong($ CYTypeVariable("int")))));
62 case sig::ushort_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeShort($ CYTypeVariable("int"))));
63 case sig::array_P: return Decode(pool, type->data.data.type)->Modify($ CYTypeArrayOf($D(type->data.data.size)));
64
65 case sig::pointer_P: {
66 CYTypedIdentifier *typed;
67 if (type->data.data.type == NULL)
68 typed = $ CYTypedIdentifier($ CYTypeVoid());
69 else
70 typed = Decode(pool, type->data.data.type);
71 return typed->Modify($ CYTypePointerTo());
72 } break;
73
74 case sig::bit_P: _assert(false); break;
75 case sig::char_P: return $ CYTypedIdentifier($ CYTypeVariable("char"));
76 case sig::double_P: return $ CYTypedIdentifier($ CYTypeVariable("double"));
77 case sig::float_P: return $ CYTypedIdentifier($ CYTypeVariable("float"));
78 case sig::int_P: return $ CYTypedIdentifier($ CYTypeVariable("int"));
79 case sig::long_P: return $ CYTypedIdentifier($ CYTypeLong($ CYTypeVariable("int")));
80 case sig::longlong_P: return $ CYTypedIdentifier($ CYTypeLong($ CYTypeLong($ CYTypeVariable("int"))));
81 case sig::short_P: return $ CYTypedIdentifier($ CYTypeShort($ CYTypeVariable("int")));
82
83 case sig::void_P: return $ CYTypedIdentifier($ CYTypeVoid());
84
85 case sig::struct_P: {
86 _assert(type->name != NULL);
87 return $ CYTypedIdentifier($ CYTypeVariable(type->name));
88 } break;
89 }
90
91 _assert(false);
92 return NULL;
93 }
94
95 CYTypedIdentifier *Decode(CYPool &pool, struct sig::Type *type) {
96 CYTypedIdentifier *typed(Decode_(pool, type));
97 if ((type->flags & JOC_TYPE_CONST) != 0) {
98 if (type->primitive == sig::string_P)
99 typed->modifier_ = $ CYTypeConstant(typed->modifier_);
100 else
101 typed = typed->Modify($ CYTypeConstant());
102 }
103 return typed;
104 }