]>
Commit | Line | Data |
---|---|---|
7341eedb JF |
1 | /* Cycript - The Truly Universal Scripting Language |
2 | * Copyright (C) 2009-2016 Jay Freeman (saurik) | |
b4aa79af JF |
3 | */ |
4 | ||
f95d2598 | 5 | /* GNU Affero General Public License, Version 3 {{{ */ |
b4aa79af | 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 | |
c15969fd | 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/>. | |
b3378a02 | 19 | **/ |
b4aa79af JF |
20 | /* }}} */ |
21 | ||
ea2d184c | 22 | #include "sig/parse.hpp" |
37954781 JF |
23 | #include "Error.hpp" |
24 | ||
25 | #include <cstdio> | |
26 | #include <cstdlib> | |
27 | #include <cstring> | |
9a39f705 | 28 | #include <sstream> |
ea2d184c JF |
29 | |
30 | namespace sig { | |
31 | ||
b799113b JF |
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); | |
b21525c7 JF |
34 | |
35 | ||
ea2d184c | 36 | /* XXX: I really screwed up this time */ |
b799113b | 37 | void *prealloc_(CYPool &pool, void *odata, size_t osize, size_t nsize) { |
0cbeddf8 | 38 | void *ndata(pool.malloc<void>(nsize)); |
ea2d184c JF |
39 | memcpy(ndata, odata, osize); |
40 | return ndata; | |
41 | } | |
42 | ||
b799113b | 43 | void Parse_(CYPool &pool, struct Signature *signature, const char **name, char eos, Callback callback) { |
ea2d184c JF |
44 | _assert(*name != NULL); |
45 | ||
f33b048a JF |
46 | // XXX: this is just a stupid check :( |
47 | bool named(**name == '"'); | |
ea2d184c JF |
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 { | |
a815a4d6 | 66 | const char *quote = strchr(++*name, '"'); |
b799113b | 67 | element->name = pool.strmemdup(*name, quote - *name); |
ea2d184c JF |
68 | *name = quote + 1; |
69 | } | |
70 | ||
f33b048a | 71 | element->type = Parse_(pool, name, eos, named, callback); |
ea2d184c JF |
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 | ||
0559abf8 JF |
85 | Type *Parse_(CYPool &pool, const char **encoding, char eos, bool named, Callback callback) { |
86 | char next = *(*encoding)++; | |
ea2d184c | 87 | |
0559abf8 JF |
88 | Type *type; |
89 | uint8_t flags(0); | |
ea2d184c JF |
90 | |
91 | parse: | |
92 | switch (next) { | |
0559abf8 | 93 | case '?': type = new(pool) Unknown(); break; |
e2ce853b JF |
94 | |
95 | #ifdef CY_OBJECTIVEC | |
0559abf8 | 96 | case '#': type = new(pool) Meta(); break; |
e2ce853b | 97 | #endif |
ea2d184c JF |
98 | |
99 | case '(': | |
0559abf8 | 100 | type = new(pool) Aggregate(true); |
ea2d184c JF |
101 | next = ')'; |
102 | goto aggregate; | |
103 | ||
0559abf8 | 104 | case '*': type = new(pool) String(); break; |
e2ce853b JF |
105 | |
106 | #ifdef CY_OBJECTIVEC | |
0559abf8 | 107 | case ':': type = new(pool) Selector(); break; |
ea2d184c | 108 | |
ecf94af8 | 109 | case '@': { |
0559abf8 | 110 | char next(**encoding); |
ecf94af8 JF |
111 | |
112 | if (next == '?') { | |
0559abf8 JF |
113 | type = new(pool) Block(); |
114 | ++*encoding; | |
ecf94af8 | 115 | } else { |
0559abf8 JF |
116 | const char *name; |
117 | if (next != '"') | |
118 | name = NULL; | |
119 | else { | |
120 | const char *quote = strchr(*encoding + 1, '"'); | |
506aad76 | 121 | if (quote == NULL) { |
0559abf8 | 122 | printf("unterminated specific id type {%s}\n", *encoding - 10); |
506aad76 JF |
123 | _assert(false); |
124 | } else if (!named || quote[1] == eos || quote[1] == '"') { | |
0559abf8 JF |
125 | name = pool.strmemdup(*encoding + 1, quote - *encoding - 1); |
126 | *encoding = quote + 1; | |
127 | } else { | |
128 | name = NULL; | |
ecf94af8 | 129 | } |
ea2d184c | 130 | } |
0559abf8 JF |
131 | |
132 | type = new(pool) Object(name); | |
ea2d184c JF |
133 | } |
134 | ||
ecf94af8 | 135 | } break; |
e2ce853b | 136 | #endif |
ea2d184c | 137 | |
0559abf8 JF |
138 | case 'B': type = new(pool) Primitive<bool>(); break; |
139 | case 'C': type = new(pool) Primitive<unsigned char>(); break; | |
140 | case 'I': type = new(pool) Primitive<unsigned int>(); break; | |
141 | case 'L': type = new(pool) Primitive<unsigned long>(); break; | |
142 | case 'Q': type = new(pool) Primitive<unsigned long long>(); break; | |
143 | case 'S': type = new(pool) Primitive<unsigned short>(); break; | |
144 | ||
145 | case '[': { | |
146 | size_t size(strtoul(*encoding, (char **) encoding, 10)); | |
147 | type = new(pool) Array(*Parse_(pool, encoding, eos, false, callback), size); | |
148 | if (**encoding != ']') { | |
149 | printf("']' != \"%s\"\n", *encoding); | |
ea2d184c JF |
150 | _assert(false); |
151 | } | |
0559abf8 JF |
152 | ++*encoding; |
153 | } break; | |
ea2d184c JF |
154 | |
155 | case '^': | |
0559abf8 JF |
156 | if (**encoding == '"') |
157 | _assert(false); // XXX: why is this here?!? | |
158 | else { | |
159 | type = Parse_(pool, encoding, eos, named, callback); | |
e2ce853b | 160 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
161 | Aggregate *aggregate(dynamic_cast<Aggregate *>(type)); |
162 | if (aggregate != NULL && strcmp(aggregate->name, "_objc_class") == 0) | |
163 | type = new(pool) Meta(); | |
164 | else | |
e2ce853b | 165 | #endif |
0559abf8 JF |
166 | type = new(pool) Pointer(*type); |
167 | } | |
ea2d184c JF |
168 | break; |
169 | ||
170 | case 'b': | |
0559abf8 | 171 | type = new(pool) Bits(strtoul(*encoding, (char **) encoding, 10)); |
ea2d184c JF |
172 | break; |
173 | ||
0559abf8 JF |
174 | case 'c': type = new(pool) Primitive<signed char>(); break; |
175 | case 'd': type = new(pool) Primitive<double>(); break; | |
176 | case 'f': type = new(pool) Primitive<float>(); break; | |
177 | case 'i': type = new(pool) Primitive<signed int>(); break; | |
178 | case 'l': type = new(pool) Primitive<signed long>(); break; | |
179 | case 'q': type = new(pool) Primitive<signed long long>(); break; | |
180 | case 's': type = new(pool) Primitive<short>(); break; | |
181 | case 'v': type = new(pool) Void(); break; | |
f61f9da6 | 182 | |
24ffc58c JF |
183 | #ifdef __SIZEOF_INT128__ |
184 | case 't': type = new(pool) Primitive<signed __int128>(); break; | |
185 | case 'T': type = new(pool) Primitive<unsigned __int128>(); break; | |
186 | #endif | |
187 | ||
ea2d184c | 188 | case '{': |
0559abf8 | 189 | type = new(pool) Aggregate(false); |
ea2d184c JF |
190 | next = '}'; |
191 | goto aggregate; | |
192 | ||
193 | aggregate: { | |
0559abf8 JF |
194 | Aggregate *aggregate(static_cast<Aggregate *>(type)); |
195 | ||
ea2d184c | 196 | char end = next; |
0559abf8 JF |
197 | const char *begin = *encoding; |
198 | do next = *(*encoding)++; | |
ea2d184c JF |
199 | while ( |
200 | next != '=' && | |
201 | next != '}' | |
202 | ); | |
0559abf8 | 203 | size_t length = *encoding - begin - 1; |
ea2d184c | 204 | if (strncmp(begin, "?", length) != 0) |
0559abf8 | 205 | aggregate->name = (char *) pool.strmemdup(begin, length); |
f33b048a | 206 | |
9814ec39 | 207 | if (next == '=') |
0559abf8 JF |
208 | Parse_(pool, &aggregate->signature, encoding, end, callback); |
209 | ||
210 | // XXX: this is a hack to support trivial unions | |
211 | if (aggregate->signature.count <= 1) | |
212 | aggregate->overlap = false; | |
213 | ||
214 | if (callback != NULL) | |
215 | type = (*callback)(pool, aggregate); | |
ea2d184c JF |
216 | } break; |
217 | ||
140fd60d JF |
218 | case 'r': flags |= JOC_TYPE_CONST; goto next; |
219 | ||
0559abf8 | 220 | case 'n': flags |= JOC_TYPE_IN; goto next; |
140fd60d | 221 | case 'N': flags |= JOC_TYPE_INOUT; goto next; |
0559abf8 | 222 | case 'o': flags |= JOC_TYPE_OUT; goto next; |
140fd60d | 223 | case 'O': flags |= JOC_TYPE_BYCOPY; goto next; |
0559abf8 | 224 | case 'R': flags |= JOC_TYPE_BYREF; goto next; |
0559abf8 | 225 | case 'V': flags |= JOC_TYPE_ONEWAY; goto next; |
ea2d184c JF |
226 | |
227 | next: | |
0559abf8 | 228 | next = *(*encoding)++; |
ea2d184c JF |
229 | goto parse; |
230 | break; | |
231 | ||
232 | default: | |
0559abf8 | 233 | printf("invalid type character: '%c' {%s}\n", next, *encoding - 10); |
ea2d184c JF |
234 | _assert(false); |
235 | } | |
236 | ||
0559abf8 | 237 | type->flags = flags; |
9814ec39 | 238 | |
ea2d184c JF |
239 | return type; |
240 | } | |
241 | ||
b799113b | 242 | void Parse(CYPool &pool, struct Signature *signature, const char *name, Callback callback) { |
ea2d184c | 243 | const char *temp = name; |
f33b048a | 244 | Parse_(pool, signature, &temp, '\0', callback); |
ea2d184c JF |
245 | _assert(temp[-1] == '\0'); |
246 | } | |
247 | ||
0559abf8 | 248 | const char *Unparse(CYPool &pool, const struct Signature *signature) { |
ea2d184c JF |
249 | const char *value = ""; |
250 | size_t offset; | |
251 | ||
252 | for (offset = 0; offset != signature->count; ++offset) { | |
b21525c7 | 253 | const char *type = Unparse(pool, signature->elements[offset].type); |
0cbeddf8 | 254 | value = pool.strcat(value, type, NULL); |
ea2d184c JF |
255 | } |
256 | ||
257 | return value; | |
258 | } | |
259 | ||
0559abf8 JF |
260 | template <> |
261 | const char *Primitive<bool>::Encode(CYPool &pool) const { | |
262 | return "B"; | |
263 | } | |
9a39f705 | 264 | |
0559abf8 JF |
265 | template <> |
266 | const char *Primitive<char>::Encode(CYPool &pool) const { | |
267 | return "c"; | |
268 | } | |
ea2d184c | 269 | |
0559abf8 JF |
270 | template <> |
271 | const char *Primitive<double>::Encode(CYPool &pool) const { | |
272 | return "d"; | |
273 | } | |
9a39f705 | 274 | |
0559abf8 JF |
275 | template <> |
276 | const char *Primitive<float>::Encode(CYPool &pool) const { | |
277 | return "f"; | |
278 | } | |
279 | ||
280 | template <> | |
281 | const char *Primitive<signed char>::Encode(CYPool &pool) const { | |
282 | return "c"; | |
283 | } | |
284 | ||
285 | template <> | |
286 | const char *Primitive<signed int>::Encode(CYPool &pool) const { | |
287 | return "i"; | |
288 | } | |
289 | ||
24ffc58c JF |
290 | #ifdef __SIZEOF_INT128__ |
291 | template <> | |
292 | const char *Primitive<signed __int128>::Encode(CYPool &pool) const { | |
293 | return "t"; | |
294 | } | |
295 | #endif | |
296 | ||
0559abf8 JF |
297 | template <> |
298 | const char *Primitive<signed long int>::Encode(CYPool &pool) const { | |
299 | return "l"; | |
300 | } | |
301 | ||
302 | template <> | |
303 | const char *Primitive<signed long long int>::Encode(CYPool &pool) const { | |
304 | return "q"; | |
305 | } | |
306 | ||
307 | template <> | |
308 | const char *Primitive<signed short int>::Encode(CYPool &pool) const { | |
309 | return "s"; | |
310 | } | |
311 | ||
312 | template <> | |
313 | const char *Primitive<unsigned char>::Encode(CYPool &pool) const { | |
314 | return "C"; | |
315 | } | |
316 | ||
317 | template <> | |
318 | const char *Primitive<unsigned int>::Encode(CYPool &pool) const { | |
319 | return "I"; | |
320 | } | |
321 | ||
24ffc58c JF |
322 | #ifdef __SIZEOF_INT128__ |
323 | template <> | |
324 | const char *Primitive<unsigned __int128>::Encode(CYPool &pool) const { | |
325 | return "T"; | |
326 | } | |
327 | #endif | |
328 | ||
0559abf8 JF |
329 | template <> |
330 | const char *Primitive<unsigned long int>::Encode(CYPool &pool) const { | |
331 | return "L"; | |
332 | } | |
333 | ||
334 | template <> | |
335 | const char *Primitive<unsigned long long int>::Encode(CYPool &pool) const { | |
336 | return "Q"; | |
337 | } | |
338 | ||
339 | template <> | |
340 | const char *Primitive<unsigned short int>::Encode(CYPool &pool) const { | |
341 | return "S"; | |
342 | } | |
343 | ||
344 | const char *Void::Encode(CYPool &pool) const { | |
345 | return "v"; | |
346 | } | |
347 | ||
348 | const char *Unknown::Encode(CYPool &pool) const { | |
349 | return "?"; | |
350 | } | |
351 | ||
352 | const char *String::Encode(CYPool &pool) const { | |
353 | return "*"; | |
354 | } | |
ea2d184c | 355 | |
e2ce853b | 356 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
357 | const char *Meta::Encode(CYPool &pool) const { |
358 | return "#"; | |
ea2d184c JF |
359 | } |
360 | ||
0559abf8 JF |
361 | const char *Selector::Encode(CYPool &pool) const { |
362 | return ":"; | |
363 | } | |
e2ce853b | 364 | #endif |
0559abf8 JF |
365 | |
366 | const char *Bits::Encode(CYPool &pool) const { | |
367 | return pool.strcat("b", pool.itoa(size), NULL); | |
368 | } | |
369 | ||
370 | const char *Pointer::Encode(CYPool &pool) const { | |
371 | return pool.strcat("^", type.Encode(pool), NULL); | |
372 | } | |
373 | ||
374 | const char *Array::Encode(CYPool &pool) const { | |
375 | return pool.strcat("[", pool.itoa(size), type.Encode(pool), "]", NULL); | |
376 | } | |
377 | ||
e2ce853b | 378 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
379 | const char *Object::Encode(CYPool &pool) const { |
380 | return name == NULL ? "@" : pool.strcat("@\"", name, "\"", NULL); | |
381 | } | |
e2ce853b | 382 | #endif |
0559abf8 | 383 | |
aaa29c28 JF |
384 | const char *Enum::Encode(CYPool &pool) const { |
385 | return type.Encode(pool); | |
386 | } | |
387 | ||
0559abf8 JF |
388 | const char *Aggregate::Encode(CYPool &pool) const { |
389 | return pool.strcat(overlap ? "(" : "{", name == NULL ? "?" : name, "=", Unparse(pool, &signature), overlap ? ")" : "}", NULL); | |
390 | } | |
391 | ||
392 | const char *Function::Encode(CYPool &pool) const { | |
393 | return "?"; | |
394 | } | |
395 | ||
e2ce853b | 396 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
397 | const char *Block::Encode(CYPool &pool) const { |
398 | return "@?"; | |
399 | } | |
e2ce853b | 400 | #endif |
51714815 | 401 | |
0559abf8 JF |
402 | const char *Unparse(CYPool &pool, const struct Type *type) { |
403 | const char *base(type->Encode(pool)); | |
51714815 JF |
404 | if (type->flags == 0) |
405 | return base; | |
406 | ||
407 | #define iovec_(base, size) \ | |
408 | (struct iovec) {const_cast<char *>(base), size} | |
409 | ||
0cbeddf8 JF |
410 | size_t size(strlen(base)); |
411 | char buffer[7 + size]; | |
412 | size_t offset(0); | |
51714815 JF |
413 | |
414 | if ((type->flags & JOC_TYPE_INOUT) != 0) | |
0cbeddf8 | 415 | buffer[offset++] = 'N'; |
51714815 | 416 | if ((type->flags & JOC_TYPE_IN) != 0) |
0cbeddf8 | 417 | buffer[offset++] = 'n'; |
51714815 | 418 | if ((type->flags & JOC_TYPE_BYCOPY) != 0) |
0cbeddf8 | 419 | buffer[offset++] = 'O'; |
51714815 | 420 | if ((type->flags & JOC_TYPE_OUT) != 0) |
0cbeddf8 | 421 | buffer[offset++] = 'o'; |
51714815 | 422 | if ((type->flags & JOC_TYPE_BYREF) != 0) |
0cbeddf8 | 423 | buffer[offset++] = 'R'; |
51714815 | 424 | if ((type->flags & JOC_TYPE_CONST) != 0) |
0cbeddf8 | 425 | buffer[offset++] = 'r'; |
51714815 | 426 | if ((type->flags & JOC_TYPE_ONEWAY) != 0) |
0cbeddf8 | 427 | buffer[offset++] = 'V'; |
51714815 | 428 | |
0cbeddf8 JF |
429 | memcpy(buffer + offset, base, size); |
430 | return pool.strmemdup(buffer, offset + size); | |
51714815 JF |
431 | } |
432 | ||
ea2d184c | 433 | } |