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