]> git.saurik.com Git - cycript.git/blame - Decode.cpp
De-CYNext CYExpression, fix CYCompound/Primitive.
[cycript.git] / Decode.cpp
CommitLineData
9a39f705
JF
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
27CYTypedIdentifier *Decode_(CYPool &pool, struct sig::Type *type) {
28 switch (type->primitive) {
03db6a67
JF
29 case sig::unknown_P: return $ CYTypedIdentifier($ CYTypeError());
30
9a39f705
JF
31 case sig::function_P: {
32 _assert(type->data.signature.count != 0);
33 CYTypedParameter *parameter(NULL);
34 for (size_t i(type->data.signature.count - 1); i != 0; --i)
35 parameter = $ CYTypedParameter(Decode(pool, type->data.signature.elements[i].type), parameter);
36 return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeFunctionWith(parameter));
37 } break;
38
3fe283c5 39 case sig::typename_P: return $ CYTypedIdentifier($ CYTypeVariable("Class"));
9a39f705 40 case sig::union_P: _assert(false); break;
3fe283c5
JF
41 case sig::string_P: return $ CYTypedIdentifier($ CYTypeVariable("char"), $ CYTypePointerTo());
42 case sig::selector_P: return $ CYTypedIdentifier($ CYTypeVariable("SEL"));
3fe16be7
JF
43
44 case sig::block_P: {
45 _assert(type->data.signature.count != 0);
46 CYTypedParameter *parameter(NULL);
47 for (size_t i(type->data.signature.count - 1); i != 0; --i)
48 parameter = $ CYTypedParameter(Decode(pool, type->data.signature.elements[i].type), parameter);
49 return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeBlockWith(parameter));
50 } break;
9a39f705
JF
51
52 case sig::object_P: {
53 if (type->name == NULL)
3fe283c5 54 return $ CYTypedIdentifier($ CYTypeVariable("id"));
9a39f705 55 else
3fe283c5 56 return $ CYTypedIdentifier($ CYTypeVariable(type->name), $ CYTypePointerTo());
9a39f705
JF
57 } break;
58
3fe283c5
JF
59 case sig::boolean_P: return $ CYTypedIdentifier($ CYTypeVariable("bool"));
60 case sig::uchar_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeVariable("char")));
61 case sig::uint_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeVariable("int")));
62 case sig::ulong_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeLong($ CYTypeVariable("int"))));
63 case sig::ulonglong_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeLong($ CYTypeLong($ CYTypeVariable("int")))));
64 case sig::ushort_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeShort($ CYTypeVariable("int"))));
9a39f705
JF
65 case sig::array_P: return Decode(pool, type->data.data.type)->Modify($ CYTypeArrayOf($D(type->data.data.size)));
66
9a39f705
JF
67 case sig::pointer_P: {
68 CYTypedIdentifier *typed;
69 if (type->data.data.type == NULL)
3fe283c5 70 typed = $ CYTypedIdentifier($ CYTypeVoid());
9a39f705
JF
71 else
72 typed = Decode(pool, type->data.data.type);
73 return typed->Modify($ CYTypePointerTo());
74 } break;
75
76 case sig::bit_P: _assert(false); break;
3fe283c5
JF
77 case sig::char_P: return $ CYTypedIdentifier($ CYTypeVariable("char"));
78 case sig::double_P: return $ CYTypedIdentifier($ CYTypeVariable("double"));
79 case sig::float_P: return $ CYTypedIdentifier($ CYTypeVariable("float"));
80 case sig::int_P: return $ CYTypedIdentifier($ CYTypeVariable("int"));
81 case sig::long_P: return $ CYTypedIdentifier($ CYTypeLong($ CYTypeVariable("int")));
82 case sig::longlong_P: return $ CYTypedIdentifier($ CYTypeLong($ CYTypeLong($ CYTypeVariable("int"))));
83 case sig::short_P: return $ CYTypedIdentifier($ CYTypeShort($ CYTypeVariable("int")));
9a39f705 84
3fe283c5 85 case sig::void_P: return $ CYTypedIdentifier($ CYTypeVoid());
9a39f705
JF
86
87 case sig::struct_P: {
88 _assert(type->name != NULL);
3fe283c5 89 return $ CYTypedIdentifier($ CYTypeVariable(type->name));
9a39f705
JF
90 } break;
91 }
92
93 _assert(false);
94 return NULL;
95}
96
97CYTypedIdentifier *Decode(CYPool &pool, struct sig::Type *type) {
98 CYTypedIdentifier *typed(Decode_(pool, type));
02873b72
JF
99 if ((type->flags & JOC_TYPE_CONST) != 0) {
100 if (type->primitive == sig::string_P)
101 typed->modifier_ = $ CYTypeConstant(typed->modifier_);
102 else
103 typed = typed->Modify($ CYTypeConstant());
104 }
9a39f705
JF
105 return typed;
106}