]> git.saurik.com Git - cycript.git/blob - sig/parse.cpp
Support casting any pointer to a function type.
[cycript.git] / sig / parse.cpp
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 "sig/parse.hpp"
23 #include "Error.hpp"
24
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cstring>
28 #include <sstream>
29
30 namespace sig {
31
32 void Parse_(CYPool &pool, struct Signature *signature, const char **name, char eos, Callback callback);
33 struct Type *Parse_(CYPool &pool, const char **name, char eos, bool named, Callback callback);
34
35
36 /* XXX: I really screwed up this time */
37 void *prealloc_(CYPool &pool, void *odata, size_t osize, size_t nsize) {
38 void *ndata(pool.malloc<void>(nsize));
39 memcpy(ndata, odata, osize);
40 return ndata;
41 }
42
43 void Parse_(CYPool &pool, struct Signature *signature, const char **name, char eos, Callback callback) {
44 _assert(*name != NULL);
45
46 // XXX: this is just a stupid check :(
47 bool named(**name == '"');
48
49 signature->elements = NULL;
50 signature->count = 0;
51
52 for (;;) {
53 if (**name == eos) {
54 ++*name;
55 return;
56 }
57
58 signature->elements = (struct Element *) prealloc_(pool, signature->elements, signature->count * sizeof(struct Element), (signature->count + 1) * sizeof(struct Element));
59 _assert(signature->elements != NULL);
60
61 struct Element *element = &signature->elements[signature->count++];
62
63 if (**name != '"')
64 element->name = NULL;
65 else {
66 const char *quote = strchr(++*name, '"');
67 element->name = pool.strmemdup(*name, quote - *name);
68 *name = quote + 1;
69 }
70
71 element->type = Parse_(pool, name, eos, named, callback);
72
73 if (**name < '0' || **name > '9')
74 element->offset = _not(size_t);
75 else {
76 element->offset = 0;
77
78 do
79 element->offset = element->offset * 10 + (*(*name)++ - '0');
80 while (**name >= '0' && **name <= '9');
81 }
82 }
83 }
84
85 Type *Parse_(CYPool &pool, const char **name, char eos, bool named, Callback callback) {
86 char next = *(*name)++;
87 if (next == '?')
88 return NULL;
89
90 Type *type(new(pool) Type());
91 _assert(type != NULL);
92 memset(type, 0, sizeof(Type));
93
94 parse:
95 switch (next) {
96 case '#': type->primitive = typename_P; break;
97
98 case '(':
99 if (type->data.signature.count < 2)
100 type->primitive = struct_P;
101 else
102 type->primitive = union_P;
103 next = ')';
104 goto aggregate;
105
106 case '*': type->primitive = string_P; break;
107 case ':': type->primitive = selector_P; break;
108
109 case '@': {
110 char next(**name);
111
112 if (next == '?') {
113 type->primitive = block_P;
114 ++*name;
115 } else {
116 type->primitive = object_P;
117
118 if (next == '"') {
119 const char *quote = strchr(*name + 1, '"');
120 if (quote == NULL) {
121 printf("unterminated specific id type {%s}\n", *name - 10);
122 _assert(false);
123 } else if (!named || quote[1] == eos || quote[1] == '"') {
124 type->name = pool.strmemdup(*name + 1, quote - *name - 1);
125 *name = quote + 1;
126 }
127 }
128 }
129
130 } break;
131
132 case 'B': type->primitive = boolean_P; break;
133 case 'C': type->primitive = uchar_P; break;
134 case 'I': type->primitive = uint_P; break;
135 case 'L': type->primitive = ulong_P; break;
136 case 'Q': type->primitive = ulonglong_P; break;
137 case 'S': type->primitive = ushort_P; break;
138
139 case '[':
140 type->primitive = array_P;
141 type->data.data.size = strtoul(*name, (char **) name, 10);
142 type->data.data.type = Parse_(pool, name, eos, false, callback);
143 if (**name != ']') {
144 printf("']' != \"%s\"\n", *name);
145 _assert(false);
146 }
147 ++*name;
148 break;
149
150 case '^':
151 type->primitive = pointer_P;
152 if (**name == '"') {
153 type->data.data.type = NULL;
154 } else {
155 type->data.data.type = Parse_(pool, name, eos, named, callback);
156 sig::Type *&target(type->data.data.type);
157 if (target != NULL && target->primitive == void_P)
158 target = NULL;
159 }
160 break;
161
162 case 'b':
163 type->primitive = bit_P;
164 type->data.data.size = strtoul(*name, (char **) name, 10);
165 break;
166
167 case 'c': type->primitive = char_P; break;
168 case 'd': type->primitive = double_P; break;
169 case 'f': type->primitive = float_P; break;
170 case 'i': type->primitive = int_P; break;
171 case 'l': type->primitive = long_P; break;
172 case 'q': type->primitive = longlong_P; break;
173 case 's': type->primitive = short_P; break;
174 case 'v': type->primitive = void_P; break;
175
176 #ifdef __LP64__
177 case 'F': type->primitive = double_P; break;
178 #else
179 case 'F': type->primitive = float_P; break;
180 #endif
181
182 case '{':
183 type->primitive = struct_P;
184 next = '}';
185 goto aggregate;
186
187 aggregate: {
188 char end = next;
189 const char *begin = *name;
190 do next = *(*name)++;
191 while (
192 next != '=' &&
193 next != '}'
194 );
195 size_t length = *name - begin - 1;
196 if (strncmp(begin, "?", length) != 0)
197 type->name = (char *) pool.strmemdup(begin, length);
198 else
199 type->name = NULL;
200
201 // XXX: this types thing is a throwback to JocStrap
202
203 if (next == '=')
204 Parse_(pool, &type->data.signature, name, end, callback);
205 } break;
206
207 case 'N': type->flags |= JOC_TYPE_INOUT; goto next;
208 case 'n': type->flags |= JOC_TYPE_IN; goto next;
209 case 'O': type->flags |= JOC_TYPE_BYCOPY; goto next;
210 case 'o': type->flags |= JOC_TYPE_OUT; goto next;
211 case 'R': type->flags |= JOC_TYPE_BYREF; goto next;
212 case 'r': type->flags |= JOC_TYPE_CONST; goto next;
213 case 'V': type->flags |= JOC_TYPE_ONEWAY; goto next;
214
215 next:
216 next = *(*name)++;
217 goto parse;
218 break;
219
220 default:
221 printf("invalid type character: '%c' {%s}\n", next, *name - 10);
222 _assert(false);
223 }
224
225 if (callback != NULL)
226 (*callback)(pool, type);
227
228 return type;
229 }
230
231 void Parse(CYPool &pool, struct Signature *signature, const char *name, Callback callback) {
232 const char *temp = name;
233 Parse_(pool, signature, &temp, '\0', callback);
234 _assert(temp[-1] == '\0');
235 }
236
237 const char *Unparse(CYPool &pool, struct Signature *signature) {
238 const char *value = "";
239 size_t offset;
240
241 for (offset = 0; offset != signature->count; ++offset) {
242 const char *type = Unparse(pool, signature->elements[offset].type);
243 value = pool.strcat(value, type, NULL);
244 }
245
246 return value;
247 }
248
249 const char *Unparse_(CYPool &pool, struct Type *type) {
250 switch (type->primitive) {
251 case function_P: {
252 if (type->data.signature.count == 0)
253 return "?";
254 std::ostringstream out;
255 for (size_t i(0); i != type->data.signature.count; ++i) {
256 Element &element(type->data.signature.elements[i]);
257 out << Unparse(pool, element.type);
258 if (element.offset != _not(size_t))
259 out << pool.itoa(element.offset);
260 }
261 return pool.strdup(out.str().c_str());
262 } break;
263
264 case typename_P: return "#";
265 case union_P: return pool.strcat("(", Unparse(pool, &type->data.signature), ")", NULL);
266 case string_P: return "*";
267 case selector_P: return ":";
268 case block_P: return "@?";
269 case object_P: return type->name == NULL ? "@" : pool.strcat("@\"", type->name, "\"", NULL);
270 case boolean_P: return "B";
271 case uchar_P: return "C";
272 case uint_P: return "I";
273 case ulong_P: return "L";
274 case ulonglong_P: return "Q";
275 case ushort_P: return "S";
276
277 case array_P: {
278 const char *value = Unparse(pool, type->data.data.type);
279 return pool.strcat("[", pool.itoa(type->data.data.size), value, "]", NULL);
280 } break;
281
282 case pointer_P: {
283 if (type->data.data.type == NULL)
284 return "^v";
285 else if (type->data.data.type->primitive == function_P)
286 return "^?";
287 else
288 return pool.strcat("^", Unparse(pool, type->data.data.type), NULL);
289 } break;
290
291 case bit_P: return pool.strcat("b", pool.itoa(type->data.data.size), NULL);
292 case char_P: return "c";
293 case double_P: return "d";
294 case float_P: return "f";
295 case int_P: return "i";
296 case long_P: return "l";
297 case longlong_P: return "q";
298 case short_P: return "s";
299 case void_P: return "v";
300 case struct_P: return pool.strcat("{", type->name == NULL ? "?" : type->name, "=", Unparse(pool, &type->data.signature), "}", NULL);
301 }
302
303 _assert(false);
304 return NULL;
305 }
306
307 const char *Unparse(CYPool &pool, struct Type *type) {
308 if (type == NULL)
309 return "?";
310
311 const char *base(Unparse_(pool, type));
312 if (type->flags == 0)
313 return base;
314
315 #define iovec_(base, size) \
316 (struct iovec) {const_cast<char *>(base), size}
317
318 size_t size(strlen(base));
319 char buffer[7 + size];
320 size_t offset(0);
321
322 if ((type->flags & JOC_TYPE_INOUT) != 0)
323 buffer[offset++] = 'N';
324 if ((type->flags & JOC_TYPE_IN) != 0)
325 buffer[offset++] = 'n';
326 if ((type->flags & JOC_TYPE_BYCOPY) != 0)
327 buffer[offset++] = 'O';
328 if ((type->flags & JOC_TYPE_OUT) != 0)
329 buffer[offset++] = 'o';
330 if ((type->flags & JOC_TYPE_BYREF) != 0)
331 buffer[offset++] = 'R';
332 if ((type->flags & JOC_TYPE_CONST) != 0)
333 buffer[offset++] = 'r';
334 if ((type->flags & JOC_TYPE_ONEWAY) != 0)
335 buffer[offset++] = 'V';
336
337 memcpy(buffer + offset, base, size);
338 return pool.strmemdup(buffer, offset + size);
339 }
340
341 }