]> git.saurik.com Git - cycript.git/blame - Decode.cpp
Do not use JavaVM, in case it isn't installed yet.
[cycript.git] / Decode.cpp
CommitLineData
7341eedb
JF
1/* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 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
0559abf8
JF
27namespace sig {
28
29template <>
5b4dabb2
JF
30CYType *Primitive<bool>::Decode(CYPool &pool) const {
31 return $ CYType($ CYTypeVariable("bool"));
0559abf8
JF
32}
33
34template <>
5b4dabb2
JF
35CYType *Primitive<char>::Decode(CYPool &pool) const {
36 return $ CYType($ CYTypeCharacter(CYTypeNeutral));
0559abf8
JF
37}
38
39template <>
5b4dabb2
JF
40CYType *Primitive<double>::Decode(CYPool &pool) const {
41 return $ CYType($ CYTypeVariable("double"));
0559abf8
JF
42}
43
44template <>
5b4dabb2
JF
45CYType *Primitive<float>::Decode(CYPool &pool) const {
46 return $ CYType($ CYTypeVariable("float"));
0559abf8
JF
47}
48
49template <>
5b4dabb2
JF
50CYType *Primitive<signed char>::Decode(CYPool &pool) const {
51 return $ CYType($ CYTypeCharacter(CYTypeSigned));
0559abf8
JF
52}
53
54template <>
5b4dabb2
JF
55CYType *Primitive<signed int>::Decode(CYPool &pool) const {
56 return $ CYType($ CYTypeIntegral(CYTypeSigned, 1));
0559abf8
JF
57}
58
24ffc58c
JF
59#ifdef __SIZEOF_INT128__
60template <>
5b4dabb2
JF
61CYType *Primitive<signed __int128>::Decode(CYPool &pool) const {
62 return $ CYType($ CYTypeInt128(CYTypeSigned));
24ffc58c
JF
63}
64#endif
65
0559abf8 66template <>
5b4dabb2
JF
67CYType *Primitive<signed long int>::Decode(CYPool &pool) const {
68 return $ CYType($ CYTypeIntegral(CYTypeSigned, 2));
0559abf8
JF
69}
70
71template <>
5b4dabb2
JF
72CYType *Primitive<signed long long int>::Decode(CYPool &pool) const {
73 return $ CYType($ CYTypeIntegral(CYTypeSigned, 3));
0559abf8
JF
74}
75
76template <>
5b4dabb2
JF
77CYType *Primitive<signed short int>::Decode(CYPool &pool) const {
78 return $ CYType($ CYTypeIntegral(CYTypeSigned, 0));
0559abf8
JF
79}
80
81template <>
5b4dabb2
JF
82CYType *Primitive<unsigned char>::Decode(CYPool &pool) const {
83 return $ CYType($ CYTypeCharacter(CYTypeUnsigned));
0559abf8
JF
84}
85
86template <>
5b4dabb2
JF
87CYType *Primitive<unsigned int>::Decode(CYPool &pool) const {
88 return $ CYType($ CYTypeIntegral(CYTypeUnsigned, 1));
0559abf8
JF
89}
90
24ffc58c
JF
91#ifdef __SIZEOF_INT128__
92template <>
5b4dabb2
JF
93CYType *Primitive<unsigned __int128>::Decode(CYPool &pool) const {
94 return $ CYType($ CYTypeInt128(CYTypeUnsigned));
24ffc58c
JF
95}
96#endif
97
0559abf8 98template <>
5b4dabb2
JF
99CYType *Primitive<unsigned long int>::Decode(CYPool &pool) const {
100 return $ CYType($ CYTypeIntegral(CYTypeUnsigned, 2));
0559abf8
JF
101}
102
103template <>
5b4dabb2
JF
104CYType *Primitive<unsigned long long int>::Decode(CYPool &pool) const {
105 return $ CYType($ CYTypeIntegral(CYTypeUnsigned, 3));
0559abf8
JF
106}
107
108template <>
5b4dabb2
JF
109CYType *Primitive<unsigned short int>::Decode(CYPool &pool) const {
110 return $ CYType($ CYTypeIntegral(CYTypeUnsigned, 0));
0559abf8
JF
111}
112
5b4dabb2
JF
113CYType *Void::Decode(CYPool &pool) const {
114 return $ CYType($ CYTypeVoid());
0559abf8
JF
115}
116
5b4dabb2
JF
117CYType *Unknown::Decode(CYPool &pool) const {
118 return $ CYType($ CYTypeError());
0559abf8
JF
119}
120
5b4dabb2
JF
121CYType *String::Decode(CYPool &pool) const {
122 return $ CYType($ CYTypeCharacter(CYTypeNeutral), $ CYTypePointerTo());
0559abf8 123}
9a39f705 124
e2ce853b 125#ifdef CY_OBJECTIVEC
5b4dabb2
JF
126CYType *Meta::Decode(CYPool &pool) const {
127 return $ CYType($ CYTypeVariable("Class"));
0559abf8
JF
128}
129
5b4dabb2
JF
130CYType *Selector::Decode(CYPool &pool) const {
131 return $ CYType($ CYTypeVariable("SEL"));
0559abf8 132}
e2ce853b 133#endif
0559abf8 134
5b4dabb2 135CYType *Bits::Decode(CYPool &pool) const {
9a39f705 136 _assert(false);
9a39f705
JF
137}
138
5b4dabb2 139CYType *Pointer::Decode(CYPool &pool) const {
0559abf8
JF
140 return CYDecodeType(pool, &type)->Modify($ CYTypePointerTo());
141}
142
5b4dabb2 143CYType *Array::Decode(CYPool &pool) const {
0559abf8
JF
144 return CYDecodeType(pool, &type)->Modify($ CYTypeArrayOf($D(size)));
145}
146
e2ce853b 147#ifdef CY_OBJECTIVEC
5b4dabb2 148CYType *Object::Decode(CYPool &pool) const {
0559abf8 149 if (name == NULL)
5b4dabb2 150 return $ CYType($ CYTypeVariable("id"));
0559abf8 151 else
5b4dabb2 152 return $ CYType($ CYTypeVariable(name), $ CYTypePointerTo());
0559abf8 153}
e2ce853b 154#endif
0559abf8 155
5b4dabb2 156CYType *Enum::Decode(CYPool &pool) const {
aaa29c28
JF
157 CYEnumConstant *values(NULL);
158 for (size_t i(count); i != 0; --i)
159 values = $ CYEnumConstant($I(pool.strdup(constants[i - 1].name)), $D(constants[i - 1].value), values);
160 CYIdentifier *identifier(name == NULL ? NULL : $I(name));
5b4dabb2 161 CYType *typed(type.Decode(pool));
aaa29c28 162 _assert(typed->modifier_ == NULL);
5b4dabb2 163 return $ CYType($ CYTypeEnum(identifier, typed->specifier_, values));
aaa29c28
JF
164}
165
5b4dabb2 166CYType *Aggregate::Decode(CYPool &pool) const {
0559abf8
JF
167 _assert(!overlap);
168
1fdd7c7a
JF
169 if (signature.count == _not(size_t)) {
170 _assert(name != NULL);
5b4dabb2 171 return $ CYType($ CYTypeReference(CYTypeReferenceStruct, $I($pool.strdup(name))));
1fdd7c7a
JF
172 }
173
0559abf8
JF
174 CYTypeStructField *fields(NULL);
175 for (size_t i(signature.count); i != 0; --i) {
176 sig::Element &element(signature.elements[i - 1]);
5b4dabb2 177 fields = $ CYTypeStructField(CYDecodeType(pool, element.type), element.name == NULL ? NULL : $I(element.name), fields);
0559abf8
JF
178 }
179 CYIdentifier *identifier(name == NULL ? NULL : $I(name));
5b4dabb2 180 return $ CYType($ CYTypeStruct(identifier, $ CYStructTail(fields)));
0559abf8
JF
181}
182
5b4dabb2 183CYType *Callable::Decode(CYPool &pool) const {
0559abf8 184 _assert(signature.count != 0);
574d4720 185 CYTypedParameter *parameters(NULL);
0559abf8 186 for (size_t i(signature.count - 1); i != 0; --i)
5b4dabb2 187 parameters = $ CYTypedParameter(CYDecodeType(pool, signature.elements[i].type), NULL, parameters);
574d4720
JF
188 return Modify(pool, CYDecodeType(pool, signature.elements[0].type), parameters);
189}
190
5b4dabb2 191CYType *Function::Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const {
574d4720
JF
192 return result->Modify($ CYTypeFunctionWith(variadic, parameters));
193}
194
e2ce853b 195#ifdef CY_OBJECTIVEC
5b4dabb2 196CYType *Block::Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const {
574d4720 197 return result->Modify($ CYTypeBlockWith(parameters));
0559abf8
JF
198}
199
5b4dabb2 200CYType *Block::Decode(CYPool &pool) const {
0559abf8 201 if (signature.count == 0)
5b4dabb2 202 return $ CYType($ CYTypeVariable("NSBlock"), $ CYTypePointerTo());
574d4720 203 return Callable::Decode(pool);
0559abf8 204}
e2ce853b 205#endif
0559abf8
JF
206
207}
208
5b4dabb2
JF
209CYType *CYDecodeType(CYPool &pool, struct sig::Type *type) {
210 CYType *typed(type->Decode(pool));
02873b72 211 if ((type->flags & JOC_TYPE_CONST) != 0) {
0559abf8 212 if (dynamic_cast<sig::String *>(type) != NULL)
02873b72
JF
213 typed->modifier_ = $ CYTypeConstant(typed->modifier_);
214 else
215 typed = typed->Modify($ CYTypeConstant());
216 }
9a39f705
JF
217 return typed;
218}