]>
git.saurik.com Git - cycript.git/blob - Decode.cpp
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
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/>.
25 #include "Replace.hpp"
30 CYTypedIdentifier
*Primitive
<bool>::Decode(CYPool
&pool
) const {
31 return $
CYTypedIdentifier($
CYTypeVariable("bool"));
35 CYTypedIdentifier
*Primitive
<char>::Decode(CYPool
&pool
) const {
36 return $
CYTypedIdentifier($
CYTypeCharacter(CYTypeNeutral
));
40 CYTypedIdentifier
*Primitive
<double>::Decode(CYPool
&pool
) const {
41 return $
CYTypedIdentifier($
CYTypeVariable("double"));
45 CYTypedIdentifier
*Primitive
<float>::Decode(CYPool
&pool
) const {
46 return $
CYTypedIdentifier($
CYTypeVariable("float"));
50 CYTypedIdentifier
*Primitive
<signed char>::Decode(CYPool
&pool
) const {
51 return $
CYTypedIdentifier($
CYTypeCharacter(CYTypeSigned
));
55 CYTypedIdentifier
*Primitive
<signed int>::Decode(CYPool
&pool
) const {
56 return $
CYTypedIdentifier($
CYTypeIntegral(CYTypeSigned
, 1));
59 #ifdef __SIZEOF_INT128__
61 CYTypedIdentifier
*Primitive
<signed __int128
>::Decode(CYPool
&pool
) const {
62 return $
CYTypedIdentifier($
CYTypeInt128(CYTypeSigned
));
67 CYTypedIdentifier
*Primitive
<signed long int>::Decode(CYPool
&pool
) const {
68 return $
CYTypedIdentifier($
CYTypeIntegral(CYTypeSigned
, 2));
72 CYTypedIdentifier
*Primitive
<signed long long int>::Decode(CYPool
&pool
) const {
73 return $
CYTypedIdentifier($
CYTypeIntegral(CYTypeSigned
, 3));
77 CYTypedIdentifier
*Primitive
<signed short int>::Decode(CYPool
&pool
) const {
78 return $
CYTypedIdentifier($
CYTypeIntegral(CYTypeSigned
, 0));
82 CYTypedIdentifier
*Primitive
<unsigned char>::Decode(CYPool
&pool
) const {
83 return $
CYTypedIdentifier($
CYTypeCharacter(CYTypeUnsigned
));
87 CYTypedIdentifier
*Primitive
<unsigned int>::Decode(CYPool
&pool
) const {
88 return $
CYTypedIdentifier($
CYTypeIntegral(CYTypeUnsigned
, 1));
91 #ifdef __SIZEOF_INT128__
93 CYTypedIdentifier
*Primitive
<unsigned __int128
>::Decode(CYPool
&pool
) const {
94 return $
CYTypedIdentifier($
CYTypeInt128(CYTypeUnsigned
));
99 CYTypedIdentifier
*Primitive
<unsigned long int>::Decode(CYPool
&pool
) const {
100 return $
CYTypedIdentifier($
CYTypeIntegral(CYTypeUnsigned
, 2));
104 CYTypedIdentifier
*Primitive
<unsigned long long int>::Decode(CYPool
&pool
) const {
105 return $
CYTypedIdentifier($
CYTypeIntegral(CYTypeUnsigned
, 3));
109 CYTypedIdentifier
*Primitive
<unsigned short int>::Decode(CYPool
&pool
) const {
110 return $
CYTypedIdentifier($
CYTypeIntegral(CYTypeUnsigned
, 0));
113 CYTypedIdentifier
*Void::Decode(CYPool
&pool
) const {
114 return $
CYTypedIdentifier($
CYTypeVoid());
117 CYTypedIdentifier
*Unknown::Decode(CYPool
&pool
) const {
118 return $
CYTypedIdentifier($
CYTypeError());
121 CYTypedIdentifier
*String::Decode(CYPool
&pool
) const {
122 return $
CYTypedIdentifier($
CYTypeCharacter(CYTypeNeutral
), $
CYTypePointerTo());
126 CYTypedIdentifier
*Meta::Decode(CYPool
&pool
) const {
127 return $
CYTypedIdentifier($
CYTypeVariable("Class"));
130 CYTypedIdentifier
*Selector::Decode(CYPool
&pool
) const {
131 return $
CYTypedIdentifier($
CYTypeVariable("SEL"));
135 CYTypedIdentifier
*Bits::Decode(CYPool
&pool
) const {
139 CYTypedIdentifier
*Pointer::Decode(CYPool
&pool
) const {
140 return CYDecodeType(pool
, &type
)->Modify($
CYTypePointerTo());
143 CYTypedIdentifier
*Array::Decode(CYPool
&pool
) const {
144 return CYDecodeType(pool
, &type
)->Modify($
CYTypeArrayOf($
D(size
)));
148 CYTypedIdentifier
*Object::Decode(CYPool
&pool
) const {
150 return $
CYTypedIdentifier($
CYTypeVariable("id"));
152 return $
CYTypedIdentifier($
CYTypeVariable(name
), $
CYTypePointerTo());
156 CYTypedIdentifier
*Enum::Decode(CYPool
&pool
) const {
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
));
161 CYTypedIdentifier
*typed(type
.Decode(pool
));
162 _assert(typed
->modifier_
== NULL
);
163 return $
CYTypedIdentifier($
CYTypeEnum(identifier
, typed
->specifier_
, values
));
166 CYTypedIdentifier
*Aggregate::Decode(CYPool
&pool
) const {
169 CYTypeStructField
*fields(NULL
);
170 for (size_t i(signature
.count
); i
!= 0; --i
) {
171 sig::Element
&element(signature
.elements
[i
- 1]);
172 CYTypedIdentifier
*typed(CYDecodeType(pool
, element
.type
));
173 if (element
.name
!= NULL
)
174 typed
->identifier_
= $
I(element
.name
);
175 fields
= $
CYTypeStructField(typed
, fields
);
177 CYIdentifier
*identifier(name
== NULL
? NULL
: $
I(name
));
178 return $
CYTypedIdentifier($
CYTypeStruct(identifier
, $
CYStructTail(fields
)));
181 CYTypedIdentifier
*Callable::Decode(CYPool
&pool
) const {
182 _assert(signature
.count
!= 0);
183 CYTypedParameter
*parameters(NULL
);
184 for (size_t i(signature
.count
- 1); i
!= 0; --i
)
185 parameters
= $
CYTypedParameter(CYDecodeType(pool
, signature
.elements
[i
].type
), parameters
);
186 return Modify(pool
, CYDecodeType(pool
, signature
.elements
[0].type
), parameters
);
189 CYTypedIdentifier
*Function::Modify(CYPool
&pool
, CYTypedIdentifier
*result
, CYTypedParameter
*parameters
) const {
190 return result
->Modify($
CYTypeFunctionWith(variadic
, parameters
));
194 CYTypedIdentifier
*Block::Modify(CYPool
&pool
, CYTypedIdentifier
*result
, CYTypedParameter
*parameters
) const {
195 return result
->Modify($
CYTypeBlockWith(parameters
));
198 CYTypedIdentifier
*Block::Decode(CYPool
&pool
) const {
199 if (signature
.count
== 0)
200 return $
CYTypedIdentifier($
CYTypeVariable("NSBlock"), $
CYTypePointerTo());
201 return Callable::Decode(pool
);
207 CYTypedIdentifier
*CYDecodeType(CYPool
&pool
, struct sig::Type
*type
) {
208 CYTypedIdentifier
*typed(type
->Decode(pool
));
209 if ((type
->flags
& JOC_TYPE_CONST
) != 0) {
210 if (dynamic_cast<sig::String
*>(type
) != NULL
)
211 typed
->modifier_
= $
CYTypeConstant(typed
->modifier_
);
213 typed
= typed
->Modify($
CYTypeConstant());