]> git.saurik.com Git - cycript.git/blame - Decode.cpp
Use Lex rules to guide let [/{ parse in for loops.
[cycript.git] / Decode.cpp
CommitLineData
9a39f705 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c1d3e52e 2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
9a39f705
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
9a39f705 6/*
f95d2598
JF
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9a39f705 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9a39f705
JF
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: {
fb486b84
JF
45 if (type->data.signature.count == 0)
46 return $ CYTypedIdentifier($ CYTypeVariable("NSBlock"), $ CYTypePointerTo());
47 else {
48 CYTypedParameter *parameter(NULL);
49 for (size_t i(type->data.signature.count - 1); i != 0; --i)
50 parameter = $ CYTypedParameter(Decode(pool, type->data.signature.elements[i].type), parameter);
51 return Decode(pool, type->data.signature.elements[0].type)->Modify($ CYTypeBlockWith(parameter));
52 }
3fe16be7 53 } break;
9a39f705
JF
54
55 case sig::object_P: {
56 if (type->name == NULL)
3fe283c5 57 return $ CYTypedIdentifier($ CYTypeVariable("id"));
9a39f705 58 else
3fe283c5 59 return $ CYTypedIdentifier($ CYTypeVariable(type->name), $ CYTypePointerTo());
9a39f705
JF
60 } break;
61
3fe283c5
JF
62 case sig::boolean_P: return $ CYTypedIdentifier($ CYTypeVariable("bool"));
63 case sig::uchar_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeVariable("char")));
64 case sig::uint_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeVariable("int")));
65 case sig::ulong_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeLong($ CYTypeVariable("int"))));
66 case sig::ulonglong_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeLong($ CYTypeLong($ CYTypeVariable("int")))));
67 case sig::ushort_P: return $ CYTypedIdentifier($ CYTypeUnsigned($ CYTypeShort($ CYTypeVariable("int"))));
9a39f705
JF
68 case sig::array_P: return Decode(pool, type->data.data.type)->Modify($ CYTypeArrayOf($D(type->data.data.size)));
69
9a39f705
JF
70 case sig::pointer_P: {
71 CYTypedIdentifier *typed;
72 if (type->data.data.type == NULL)
3fe283c5 73 typed = $ CYTypedIdentifier($ CYTypeVoid());
9a39f705
JF
74 else
75 typed = Decode(pool, type->data.data.type);
76 return typed->Modify($ CYTypePointerTo());
77 } break;
78
79 case sig::bit_P: _assert(false); break;
3fe283c5
JF
80 case sig::char_P: return $ CYTypedIdentifier($ CYTypeVariable("char"));
81 case sig::double_P: return $ CYTypedIdentifier($ CYTypeVariable("double"));
82 case sig::float_P: return $ CYTypedIdentifier($ CYTypeVariable("float"));
83 case sig::int_P: return $ CYTypedIdentifier($ CYTypeVariable("int"));
84 case sig::long_P: return $ CYTypedIdentifier($ CYTypeLong($ CYTypeVariable("int")));
85 case sig::longlong_P: return $ CYTypedIdentifier($ CYTypeLong($ CYTypeLong($ CYTypeVariable("int"))));
86 case sig::short_P: return $ CYTypedIdentifier($ CYTypeShort($ CYTypeVariable("int")));
9a39f705 87
3fe283c5 88 case sig::void_P: return $ CYTypedIdentifier($ CYTypeVoid());
9a39f705
JF
89
90 case sig::struct_P: {
91 _assert(type->name != NULL);
3fe283c5 92 return $ CYTypedIdentifier($ CYTypeVariable(type->name));
9a39f705
JF
93 } break;
94 }
95
96 _assert(false);
97 return NULL;
98}
99
100CYTypedIdentifier *Decode(CYPool &pool, struct sig::Type *type) {
101 CYTypedIdentifier *typed(Decode_(pool, type));
02873b72
JF
102 if ((type->flags & JOC_TYPE_CONST) != 0) {
103 if (type->primitive == sig::string_P)
104 typed->modifier_ = $ CYTypeConstant(typed->modifier_);
105 else
106 typed = typed->Modify($ CYTypeConstant());
107 }
9a39f705
JF
108 return typed;
109}