]>
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, '"'); | |
7af82264 JF |
121 | if (quote == NULL) |
122 | CYThrow("unterminated specific id type {%s}", *encoding - 10); | |
123 | else if (!named || quote[1] == eos || quote[1] == '"') { | |
0559abf8 JF |
124 | name = pool.strmemdup(*encoding + 1, quote - *encoding - 1); |
125 | *encoding = quote + 1; | |
126 | } else { | |
127 | name = NULL; | |
ecf94af8 | 128 | } |
ea2d184c | 129 | } |
0559abf8 JF |
130 | |
131 | type = new(pool) Object(name); | |
ea2d184c JF |
132 | } |
133 | ||
ecf94af8 | 134 | } break; |
e2ce853b | 135 | #endif |
ea2d184c | 136 | |
0559abf8 JF |
137 | case 'B': type = new(pool) Primitive<bool>(); break; |
138 | case 'C': type = new(pool) Primitive<unsigned char>(); break; | |
139 | case 'I': type = new(pool) Primitive<unsigned int>(); break; | |
140 | case 'L': type = new(pool) Primitive<unsigned long>(); break; | |
141 | case 'Q': type = new(pool) Primitive<unsigned long long>(); break; | |
142 | case 'S': type = new(pool) Primitive<unsigned short>(); break; | |
143 | ||
144 | case '[': { | |
145 | size_t size(strtoul(*encoding, (char **) encoding, 10)); | |
146 | type = new(pool) Array(*Parse_(pool, encoding, eos, false, callback), size); | |
7af82264 JF |
147 | if (**encoding != ']') |
148 | CYThrow("']' != \"%s\"", *encoding); | |
0559abf8 JF |
149 | ++*encoding; |
150 | } break; | |
ea2d184c JF |
151 | |
152 | case '^': | |
0559abf8 JF |
153 | if (**encoding == '"') |
154 | _assert(false); // XXX: why is this here?!? | |
155 | else { | |
156 | type = Parse_(pool, encoding, eos, named, callback); | |
e2ce853b | 157 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
158 | Aggregate *aggregate(dynamic_cast<Aggregate *>(type)); |
159 | if (aggregate != NULL && strcmp(aggregate->name, "_objc_class") == 0) | |
160 | type = new(pool) Meta(); | |
161 | else | |
e2ce853b | 162 | #endif |
0559abf8 JF |
163 | type = new(pool) Pointer(*type); |
164 | } | |
ea2d184c JF |
165 | break; |
166 | ||
167 | case 'b': | |
0559abf8 | 168 | type = new(pool) Bits(strtoul(*encoding, (char **) encoding, 10)); |
ea2d184c JF |
169 | break; |
170 | ||
0559abf8 JF |
171 | case 'c': type = new(pool) Primitive<signed char>(); break; |
172 | case 'd': type = new(pool) Primitive<double>(); break; | |
173 | case 'f': type = new(pool) Primitive<float>(); break; | |
174 | case 'i': type = new(pool) Primitive<signed int>(); break; | |
175 | case 'l': type = new(pool) Primitive<signed long>(); break; | |
176 | case 'q': type = new(pool) Primitive<signed long long>(); break; | |
177 | case 's': type = new(pool) Primitive<short>(); break; | |
178 | case 'v': type = new(pool) Void(); break; | |
f61f9da6 | 179 | |
24ffc58c JF |
180 | #ifdef __SIZEOF_INT128__ |
181 | case 't': type = new(pool) Primitive<signed __int128>(); break; | |
182 | case 'T': type = new(pool) Primitive<unsigned __int128>(); break; | |
183 | #endif | |
184 | ||
ea2d184c | 185 | case '{': |
0559abf8 | 186 | type = new(pool) Aggregate(false); |
ea2d184c JF |
187 | next = '}'; |
188 | goto aggregate; | |
189 | ||
190 | aggregate: { | |
0559abf8 JF |
191 | Aggregate *aggregate(static_cast<Aggregate *>(type)); |
192 | ||
ea2d184c | 193 | char end = next; |
0559abf8 | 194 | const char *begin = *encoding; |
1fdd7c7a JF |
195 | do switch (next = *(*encoding)++) { |
196 | case '\0': | |
197 | _assert(false); | |
198 | case '}': | |
199 | // XXX: this is actually a type reference | |
200 | aggregate->signature.count = _not(size_t); | |
201 | next = '='; // this is a "break". I'm sorry | |
202 | } while (next != '='); | |
203 | ||
0559abf8 | 204 | size_t length = *encoding - begin - 1; |
ea2d184c | 205 | if (strncmp(begin, "?", length) != 0) |
0559abf8 | 206 | aggregate->name = (char *) pool.strmemdup(begin, length); |
f33b048a | 207 | |
1fdd7c7a JF |
208 | if (aggregate->signature.count == _not(size_t)) |
209 | aggregate->signature.elements = NULL; | |
210 | else | |
0559abf8 JF |
211 | Parse_(pool, &aggregate->signature, encoding, end, callback); |
212 | ||
213 | // XXX: this is a hack to support trivial unions | |
214 | if (aggregate->signature.count <= 1) | |
215 | aggregate->overlap = false; | |
216 | ||
217 | if (callback != NULL) | |
218 | type = (*callback)(pool, aggregate); | |
ea2d184c JF |
219 | } break; |
220 | ||
140fd60d JF |
221 | case 'r': flags |= JOC_TYPE_CONST; goto next; |
222 | ||
0559abf8 | 223 | case 'n': flags |= JOC_TYPE_IN; goto next; |
140fd60d | 224 | case 'N': flags |= JOC_TYPE_INOUT; goto next; |
0559abf8 | 225 | case 'o': flags |= JOC_TYPE_OUT; goto next; |
140fd60d | 226 | case 'O': flags |= JOC_TYPE_BYCOPY; goto next; |
0559abf8 | 227 | case 'R': flags |= JOC_TYPE_BYREF; goto next; |
0559abf8 | 228 | case 'V': flags |= JOC_TYPE_ONEWAY; goto next; |
ea2d184c JF |
229 | |
230 | next: | |
0559abf8 | 231 | next = *(*encoding)++; |
ea2d184c JF |
232 | goto parse; |
233 | break; | |
234 | ||
235 | default: | |
7af82264 | 236 | CYThrow("invalid type character: '%c' {%s}", next, *encoding - 10); |
ea2d184c JF |
237 | } |
238 | ||
0559abf8 | 239 | type->flags = flags; |
9814ec39 | 240 | |
ea2d184c JF |
241 | return type; |
242 | } | |
243 | ||
b799113b | 244 | void Parse(CYPool &pool, struct Signature *signature, const char *name, Callback callback) { |
ea2d184c | 245 | const char *temp = name; |
f33b048a | 246 | Parse_(pool, signature, &temp, '\0', callback); |
ea2d184c JF |
247 | _assert(temp[-1] == '\0'); |
248 | } | |
249 | ||
0559abf8 | 250 | const char *Unparse(CYPool &pool, const struct Signature *signature) { |
ea2d184c JF |
251 | const char *value = ""; |
252 | size_t offset; | |
253 | ||
254 | for (offset = 0; offset != signature->count; ++offset) { | |
b21525c7 | 255 | const char *type = Unparse(pool, signature->elements[offset].type); |
0cbeddf8 | 256 | value = pool.strcat(value, type, NULL); |
ea2d184c JF |
257 | } |
258 | ||
259 | return value; | |
260 | } | |
261 | ||
0559abf8 JF |
262 | template <> |
263 | const char *Primitive<bool>::Encode(CYPool &pool) const { | |
264 | return "B"; | |
265 | } | |
9a39f705 | 266 | |
0559abf8 JF |
267 | template <> |
268 | const char *Primitive<char>::Encode(CYPool &pool) const { | |
269 | return "c"; | |
270 | } | |
ea2d184c | 271 | |
0559abf8 JF |
272 | template <> |
273 | const char *Primitive<double>::Encode(CYPool &pool) const { | |
274 | return "d"; | |
275 | } | |
9a39f705 | 276 | |
0559abf8 JF |
277 | template <> |
278 | const char *Primitive<float>::Encode(CYPool &pool) const { | |
279 | return "f"; | |
280 | } | |
281 | ||
282 | template <> | |
283 | const char *Primitive<signed char>::Encode(CYPool &pool) const { | |
284 | return "c"; | |
285 | } | |
286 | ||
287 | template <> | |
288 | const char *Primitive<signed int>::Encode(CYPool &pool) const { | |
289 | return "i"; | |
290 | } | |
291 | ||
24ffc58c JF |
292 | #ifdef __SIZEOF_INT128__ |
293 | template <> | |
294 | const char *Primitive<signed __int128>::Encode(CYPool &pool) const { | |
295 | return "t"; | |
296 | } | |
297 | #endif | |
298 | ||
0559abf8 JF |
299 | template <> |
300 | const char *Primitive<signed long int>::Encode(CYPool &pool) const { | |
301 | return "l"; | |
302 | } | |
303 | ||
304 | template <> | |
305 | const char *Primitive<signed long long int>::Encode(CYPool &pool) const { | |
306 | return "q"; | |
307 | } | |
308 | ||
309 | template <> | |
310 | const char *Primitive<signed short int>::Encode(CYPool &pool) const { | |
311 | return "s"; | |
312 | } | |
313 | ||
314 | template <> | |
315 | const char *Primitive<unsigned char>::Encode(CYPool &pool) const { | |
316 | return "C"; | |
317 | } | |
318 | ||
319 | template <> | |
320 | const char *Primitive<unsigned int>::Encode(CYPool &pool) const { | |
321 | return "I"; | |
322 | } | |
323 | ||
24ffc58c JF |
324 | #ifdef __SIZEOF_INT128__ |
325 | template <> | |
326 | const char *Primitive<unsigned __int128>::Encode(CYPool &pool) const { | |
327 | return "T"; | |
328 | } | |
329 | #endif | |
330 | ||
0559abf8 JF |
331 | template <> |
332 | const char *Primitive<unsigned long int>::Encode(CYPool &pool) const { | |
333 | return "L"; | |
334 | } | |
335 | ||
336 | template <> | |
337 | const char *Primitive<unsigned long long int>::Encode(CYPool &pool) const { | |
338 | return "Q"; | |
339 | } | |
340 | ||
341 | template <> | |
342 | const char *Primitive<unsigned short int>::Encode(CYPool &pool) const { | |
343 | return "S"; | |
344 | } | |
345 | ||
346 | const char *Void::Encode(CYPool &pool) const { | |
347 | return "v"; | |
348 | } | |
349 | ||
350 | const char *Unknown::Encode(CYPool &pool) const { | |
351 | return "?"; | |
352 | } | |
353 | ||
354 | const char *String::Encode(CYPool &pool) const { | |
355 | return "*"; | |
356 | } | |
ea2d184c | 357 | |
e2ce853b | 358 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
359 | const char *Meta::Encode(CYPool &pool) const { |
360 | return "#"; | |
ea2d184c JF |
361 | } |
362 | ||
0559abf8 JF |
363 | const char *Selector::Encode(CYPool &pool) const { |
364 | return ":"; | |
365 | } | |
e2ce853b | 366 | #endif |
0559abf8 JF |
367 | |
368 | const char *Bits::Encode(CYPool &pool) const { | |
369 | return pool.strcat("b", pool.itoa(size), NULL); | |
370 | } | |
371 | ||
372 | const char *Pointer::Encode(CYPool &pool) const { | |
373 | return pool.strcat("^", type.Encode(pool), NULL); | |
374 | } | |
375 | ||
376 | const char *Array::Encode(CYPool &pool) const { | |
377 | return pool.strcat("[", pool.itoa(size), type.Encode(pool), "]", NULL); | |
378 | } | |
379 | ||
e2ce853b | 380 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
381 | const char *Object::Encode(CYPool &pool) const { |
382 | return name == NULL ? "@" : pool.strcat("@\"", name, "\"", NULL); | |
383 | } | |
e2ce853b | 384 | #endif |
0559abf8 | 385 | |
aaa29c28 JF |
386 | const char *Enum::Encode(CYPool &pool) const { |
387 | return type.Encode(pool); | |
388 | } | |
389 | ||
0559abf8 | 390 | const char *Aggregate::Encode(CYPool &pool) const { |
1fdd7c7a JF |
391 | bool reference(signature.count == _not(size_t)); |
392 | return pool.strcat(overlap ? "(" : "{", | |
393 | name == NULL ? "?" : name, | |
394 | reference ? "" : "=", | |
395 | reference ? "" : Unparse(pool, &signature), | |
396 | overlap ? ")" : "}", NULL); | |
0559abf8 JF |
397 | } |
398 | ||
399 | const char *Function::Encode(CYPool &pool) const { | |
400 | return "?"; | |
401 | } | |
402 | ||
e2ce853b | 403 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
404 | const char *Block::Encode(CYPool &pool) const { |
405 | return "@?"; | |
406 | } | |
e2ce853b | 407 | #endif |
51714815 | 408 | |
0559abf8 JF |
409 | const char *Unparse(CYPool &pool, const struct Type *type) { |
410 | const char *base(type->Encode(pool)); | |
51714815 JF |
411 | if (type->flags == 0) |
412 | return base; | |
413 | ||
414 | #define iovec_(base, size) \ | |
415 | (struct iovec) {const_cast<char *>(base), size} | |
416 | ||
0cbeddf8 JF |
417 | size_t size(strlen(base)); |
418 | char buffer[7 + size]; | |
419 | size_t offset(0); | |
51714815 JF |
420 | |
421 | if ((type->flags & JOC_TYPE_INOUT) != 0) | |
0cbeddf8 | 422 | buffer[offset++] = 'N'; |
51714815 | 423 | if ((type->flags & JOC_TYPE_IN) != 0) |
0cbeddf8 | 424 | buffer[offset++] = 'n'; |
51714815 | 425 | if ((type->flags & JOC_TYPE_BYCOPY) != 0) |
0cbeddf8 | 426 | buffer[offset++] = 'O'; |
51714815 | 427 | if ((type->flags & JOC_TYPE_OUT) != 0) |
0cbeddf8 | 428 | buffer[offset++] = 'o'; |
51714815 | 429 | if ((type->flags & JOC_TYPE_BYREF) != 0) |
0cbeddf8 | 430 | buffer[offset++] = 'R'; |
51714815 | 431 | if ((type->flags & JOC_TYPE_CONST) != 0) |
0cbeddf8 | 432 | buffer[offset++] = 'r'; |
51714815 | 433 | if ((type->flags & JOC_TYPE_ONEWAY) != 0) |
0cbeddf8 | 434 | buffer[offset++] = 'V'; |
51714815 | 435 | |
0cbeddf8 JF |
436 | memcpy(buffer + offset, base, size); |
437 | return pool.strmemdup(buffer, offset + size); | |
51714815 JF |
438 | } |
439 | ||
ea2d184c | 440 | } |