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