]>
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 CYType
*Primitive
<bool>::Decode(CYPool
&pool
) const {
31 return $
CYType($
CYTypeVariable("bool"));
35 CYType
*Primitive
<char>::Decode(CYPool
&pool
) const {
36 return $
CYType($
CYTypeCharacter(CYTypeNeutral
));
40 CYType
*Primitive
<double>::Decode(CYPool
&pool
) const {
41 return $
CYType($
CYTypeFloating(1));
45 CYType
*Primitive
<float>::Decode(CYPool
&pool
) const {
46 return $
CYType($
CYTypeFloating(0));
50 CYType
*Primitive
<long double>::Decode(CYPool
&pool
) const {
51 return $
CYType($
CYTypeFloating(2));
55 CYType
*Primitive
<signed char>::Decode(CYPool
&pool
) const {
56 return $
CYType($
CYTypeCharacter(CYTypeSigned
));
60 CYType
*Primitive
<signed int>::Decode(CYPool
&pool
) const {
61 return $
CYType($
CYTypeIntegral(CYTypeSigned
, 1));
64 #ifdef __SIZEOF_INT128__
66 CYType
*Primitive
<signed __int128
>::Decode(CYPool
&pool
) const {
67 return $
CYType($
CYTypeInt128(CYTypeSigned
));
72 CYType
*Primitive
<signed long int>::Decode(CYPool
&pool
) const {
73 return $
CYType($
CYTypeIntegral(CYTypeSigned
, 2));
77 CYType
*Primitive
<signed long long int>::Decode(CYPool
&pool
) const {
78 return $
CYType($
CYTypeIntegral(CYTypeSigned
, 3));
82 CYType
*Primitive
<signed short int>::Decode(CYPool
&pool
) const {
83 return $
CYType($
CYTypeIntegral(CYTypeSigned
, 0));
87 CYType
*Primitive
<unsigned char>::Decode(CYPool
&pool
) const {
88 return $
CYType($
CYTypeCharacter(CYTypeUnsigned
));
92 CYType
*Primitive
<unsigned int>::Decode(CYPool
&pool
) const {
93 return $
CYType($
CYTypeIntegral(CYTypeUnsigned
, 1));
96 #ifdef __SIZEOF_INT128__
98 CYType
*Primitive
<unsigned __int128
>::Decode(CYPool
&pool
) const {
99 return $
CYType($
CYTypeInt128(CYTypeUnsigned
));
104 CYType
*Primitive
<unsigned long int>::Decode(CYPool
&pool
) const {
105 return $
CYType($
CYTypeIntegral(CYTypeUnsigned
, 2));
109 CYType
*Primitive
<unsigned long long int>::Decode(CYPool
&pool
) const {
110 return $
CYType($
CYTypeIntegral(CYTypeUnsigned
, 3));
114 CYType
*Primitive
<unsigned short int>::Decode(CYPool
&pool
) const {
115 return $
CYType($
CYTypeIntegral(CYTypeUnsigned
, 0));
118 CYType
*Void::Decode(CYPool
&pool
) const {
119 return $
CYType($
CYTypeVoid());
122 CYType
*Unknown::Decode(CYPool
&pool
) const {
123 return $
CYType($
CYTypeError());
126 CYType
*String::Decode(CYPool
&pool
) const {
127 return $
CYType($
CYTypeCharacter(CYTypeNeutral
), $
CYTypePointerTo());
131 CYType
*Meta::Decode(CYPool
&pool
) const {
132 return $
CYType($
CYTypeVariable("Class"));
135 CYType
*Selector::Decode(CYPool
&pool
) const {
136 return $
CYType($
CYTypeVariable("SEL"));
140 CYType
*Bits::Decode(CYPool
&pool
) const {
144 CYType
*Pointer::Decode(CYPool
&pool
) const {
145 return CYDecodeType(pool
, &type
)->Modify($
CYTypePointerTo());
148 CYType
*Array::Decode(CYPool
&pool
) const {
149 return CYDecodeType(pool
, &type
)->Modify($
CYTypeArrayOf($
D(size
)));
153 CYType
*Object::Decode(CYPool
&pool
) const {
155 return $
CYType($
CYTypeVariable("id"));
157 return $
CYType($
CYTypeVariable(name
), $
CYTypePointerTo());
161 CYType
*Enum::Decode(CYPool
&pool
) const {
162 CYEnumConstant
*values(NULL
);
163 for (size_t i(count
); i
!= 0; --i
)
164 values
= $
CYEnumConstant($
I(pool
.strdup(constants
[i
- 1].name
)), $
D(constants
[i
- 1].value
), values
);
165 CYIdentifier
*identifier(name
== NULL
? NULL
: $
I(name
));
166 CYType
*typed(type
.Decode(pool
));
167 _assert(typed
->modifier_
== NULL
);
168 return $
CYType($
CYTypeEnum(identifier
, typed
->specifier_
, values
));
171 CYType
*Aggregate::Decode(CYPool
&pool
) const {
174 if (signature
.count
== _not(size_t)) {
175 _assert(name
!= NULL
);
176 return $
CYType($
CYTypeReference(CYTypeReferenceStruct
, $
I($pool
.strdup(name
))));
179 CYTypeStructField
*fields(NULL
);
180 for (size_t i(signature
.count
); i
!= 0; --i
) {
181 sig::Element
&element(signature
.elements
[i
- 1]);
182 fields
= $
CYTypeStructField(CYDecodeType(pool
, element
.type
), element
.name
== NULL
? NULL
: $
I(element
.name
), fields
);
184 CYIdentifier
*identifier(name
== NULL
? NULL
: $
I(name
));
185 return $
CYType($
CYTypeStruct(identifier
, $
CYStructTail(fields
)));
188 CYType
*Callable::Decode(CYPool
&pool
) const {
189 _assert(signature
.count
!= 0);
190 CYTypedParameter
*parameters(NULL
);
191 for (size_t i(signature
.count
- 1); i
!= 0; --i
)
192 parameters
= $
CYTypedParameter(CYDecodeType(pool
, signature
.elements
[i
].type
), NULL
, parameters
);
193 return Modify(pool
, CYDecodeType(pool
, signature
.elements
[0].type
), parameters
);
196 CYType
*Function::Modify(CYPool
&pool
, CYType
*result
, CYTypedParameter
*parameters
) const {
197 return result
->Modify($
CYTypeFunctionWith(variadic
, parameters
));
201 CYType
*Block::Modify(CYPool
&pool
, CYType
*result
, CYTypedParameter
*parameters
) const {
202 return result
->Modify($
CYTypeBlockWith(parameters
));
205 CYType
*Block::Decode(CYPool
&pool
) const {
206 if (signature
.count
== 0)
207 return $
CYType($
CYTypeVariable("NSBlock"), $
CYTypePointerTo());
208 return Callable::Decode(pool
);
214 CYType
*CYDecodeType(CYPool
&pool
, struct sig::Type
*type
) {
215 CYType
*typed(type
->Decode(pool
));
216 if ((type
->flags
& JOC_TYPE_CONST
) != 0) {
217 if (dynamic_cast<sig::String
*>(type
) != NULL
)
218 typed
->modifier_
= $
CYTypeConstant(typed
->modifier_
);
220 typed
= typed
->Modify($
CYTypeConstant());